如何解决收到Firebase通知时无需单击通知即可打开活动
我正在构建一个具有通话功能的应用程序。因此,我从服务器收到通知,并且建立了一个活动来显示呼叫屏幕,其中有应答和挂断按钮。
现在我有一个问题,当应用程序处于后台/死机状态或手机被锁定时,应该使用什么来启动活动?
我试图在FirebaseMessagingService的onMessageRecieved函数中启动活动,但是我认为当应用程序在后台运行时不会调用该函数。因为当我在后台运行应用程序时尝试调试相同的对象时,不会触发调试指针并且活动也不会打开。
这是我的服务
class AppFirebaseMessagingService : FirebaseMessagingService() {
private var pendingIntent: PendingIntent? = null
override fun onMessageReceived(remoteMessage: RemoteMessage) {
Log.e("get_type_data",remoteMessage!!.data.toString())
val data = remoteMessage.data
val myCustomKey = data["notification_data"]
if(!myCustomKey?.length?.equals(0)!!)
{
createNotification(remoteMessage.data)
}
}
private fun createNotification(messageBody: Map<String,String>) {
val content: String? = messageBody["content"]
val id: String? = messageBody["id_orders"]
val type: String? = messageBody["type"]
var foregroud = false
try {
foregroud = ForegroundCheckTask().execute(this).get()
} catch (e: InterruptedException) {
e.printstacktrace()
} catch (e: ExecutionException) {
e.printstacktrace()
}
Log.e("foreground",foregroud.toString())
val notificationmanager = getSystemService(Context.NOTIFICATION_SERVICE) as notificationmanager
val intent: Intent
val random = Random().nextInt(1000).toString()
if (foregroud) {
// intent = Intent("NEW_ORDER")
// intent.putExtra(Cons.PREF_ORDER_ID,id)
// sendbroadcast(intent)
intent = Intent(this,NotificationActivity::class.java)
intent.addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK
or Intent.FLAG_ACTIVITY_NEW_TASK
)
pendingIntent = PendingIntent.getActivity(
this,Integer.valueOf(random)
/* Request code */,intent,PendingIntent.FLAG_UPDATE_CURRENT
)
startActivity(intent)
} else {
intent = Intent(this,PendingIntent.FLAG_UPDATE_CURRENT
)
/* intent = Intent(this,NotificationActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK
or Intent.FLAG_ACTIVITY_NEW_TASK)
pendingIntent = PendingIntent.getActivity(this,Integer.valueOf(random)
*//* Request code *//*,PendingIntent.FLAG_UPDATE_CURRENT)
startActivity(intent)*/
}
if (id != null) {
val defaultSoundUri = ringtoneManager.getDefaultUri(ringtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this,id)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(this.resources.getString(R.string.app_name))
.setContentText(content)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
id,getString(R.string.app_name),notificationmanager.IMPORTANCE_HIGH
)
channel.description = applicationContext.getString(R.string.app_name)
channel.setShowBadge(true)
channel.canShowBadge()
channel.enableLights(true)
channel.lightColor = applicationContext.getColor(R.color.colorPrimaryDark)
channel.enableVibration(true)
notificationmanager.createNotificationChannel(channel)
}
notificationmanager.notify(
Integer.parseInt(id)
/* ID of notification */,notificationBuilder.build()
)
} else {
Log.e("randomidforNotif",random)
val defaultSoundUri = ringtoneManager.getDefaultUri(ringtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this,random)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(this.resources.getString(R.string.app_name))
.setContentText(content)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
random,notificationmanager.IMPORTANCE_HIGH
)
channel.description = applicationContext.getString(R.string.app_name)
channel.setShowBadge(true)
channel.canShowBadge()
channel.enableLights(true)
channel.lightColor = applicationContext.getColor(R.color.colorPrimaryDark)
channel.enableVibration(true)
notificationmanager.createNotificationChannel(channel)
}
notificationmanager.notify(
Integer.parseInt(random)
/* ID of notification */,notificationBuilder.build()
)
}
}
@SuppressLint("StaticFieldLeak")
private inner class ForegroundCheckTask : AsyncTask<Context,Void,Boolean>() {
override fun doInBackground(vararg params: Context): Boolean? {
val context = params[0].applicationContext
return isAppOnForeground(context)
}
private fun isAppOnForeground(context: Context): Boolean {
val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
val appProcesses = activityManager.runningAppProcesses ?: return false
val packageName = context.packageName
for (appProcess in appProcesses) {
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName == packageName) {
return true
}
}
return false
}
}
override fun onNewToken(token: String) {
super.onNewToken(token)
}
}
我关注了此链接-Open activity on firebase notification received in foreground
我也尝试过-How to make a screen wake up when a notification is received? ----唤醒设备
这是我的清单文件
<uses-permission android:name="android.permission.disABLE_KEyguard" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUdio" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.MODIFY_AUdio_SETTINGS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BLUetoOTH" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.BIND_JOB_SERVICE"
tools:ignore="ProtectedPermissions" />
<uses-permission
android:name="android.permission.READ_PRIVILEGED_PHONE_STATE"
tools:ignore="ProtectedPermissions" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppThemeNoActionBar">
<activity android:name=".LauncherActivity">
</activity>
<activity
android:name=".NotificationActivity"
android:launchMode="singletop"
android:screenorientation="portrait"
android:theme="@style/AppThemeNoActionBar">
<intent-filter>
<!-- Note: these actions are notification actions -->
<action android:name="VIDEO_CALLING" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--<service
android:name=".MyFirebaseMessagingService"
android:directBootAware="true"
android:exported="false"
tools:targetApi="n">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>-->
<service
android:name=".AppFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<Meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_launcher" />
<Meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" />
<!--
<Meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
<Meta-data
android:name="firebase_messaging_auto_init_enabled"
android:value="false" />
<Meta-data
android:name="firebase_analytics_collection_enabled"
android:value="false" />
<receiver android:name=".FirebaseDataReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name=".MyJobIntentService"
android:permission="android.permission.BIND_JOB_SERVICE" />
<service android:name="com.google.android.gms.measurement.AppMeasurementService"
android:enabled="true"
android:exported="false"/>
<receiver android:name="com.google.android.gms.measurement.AppMeasurementReceiver"
android:enabled="true">
<intent-filter>
<action android:name="com.google.android.gms.measurement.UPLOAD" />
</intent-filter>
</receiver>
</application>
</manifest>
我已经添加了所有可能的权限和所需的元数据。
请协助解决。谢谢...
解决方法
在显示通知之前添加此行
notificationBuilder.setFullScreenIntent(pendingIntent,true);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。