我必须在收到推送通知时打开我的特定活动。当应用程序在前台时,我可以处理相同的操作,但是当应用程序在后台或被终止时,活动不显示。
我的通知帮助程序类
package com.oxicodes.apple.edropsStaff.firebaseservice
import android.app.Notification
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.media.MediaPlayer
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.oxicodes.apple.edropsStaff.Activity_pendingorders
import com.oxicodes.apple.edropsStaff.MainActivity
import com.oxicodes.apple.edropsStaff.R
import java.util.*
object NotificationHelper {
var pendingIntent: PendingIntent? = null
fun displayNotification(context: Context, title: String, body: String, notId: Int, foreground: Boolean) {
if (title.equals("New Order")) {
val random = Random().nextInt(1000).toString()
val mBuilder = NotificationCompat.Builder(context, MainActivity.CHANNEL_ID)
.setSmallIcon(R.drawable.ic_waterlogo)
.setContentTitle(title)
.setContentText(body)
.setStyle(NotificationCompat.BigTextStyle()
.bigText(body))
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(Notification.FLAG_ONLY_ALERT_ONCE)
val mNotificationMgr = NotificationManagerCompat.from(context)
val mp: MediaPlayer = MediaPlayer.create(context, R.raw.notificationb)
mp.start()
mNotificationMgr.notify(notId, mBuilder.build())
if (foreground) { //app in foreground
var intent = Intent(context, Activity_pendingorders::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK)
pendingIntent = PendingIntent.getActivity(context, Integer.valueOf(random) /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent) // to directly open activity if app is foreground
}
} else {
val intent = Intent(context, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(
context,
100,
intent,
PendingIntent.FLAG_CANCEL_CURRENT
)
val mBuilder = NotificationCompat.Builder(context, MainActivity.CHANNEL_ID)
.setSmallIcon(R.drawable.ic_waterlogo)
.setContentTitle(title)
.setContentText(body)
.setStyle(NotificationCompat.BigTextStyle()
.bigText(body))
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(Notification.FLAG_ONLY_ALERT_ONCE)
val mNotificationMgr = NotificationManagerCompat.from(context)
val mp: MediaPlayer = MediaPlayer.create(context, R.raw.notification)
mp.start()
mNotificationMgr.notify(notId, mBuilder.build())
}
}
}
我的Firebase消息服务类
class myfirebasemessaging : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage?) {
super.onMessageReceived(remoteMessage)
if (remoteMessage!!.data != null) {
val title = remoteMessage.data!!.get("title")
val body = remoteMessage.data!!.get("body")
val tag = remoteMessage.data!!.get("notId")!!.toInt()
var foreground = false
try {
foreground = ForegroundCheckTask().execute(this).get()
} catch (e: InterruptedException) {
e.printStackTrace()
} catch (e: ExecutionException) {
e.printStackTrace()
}
NotificationHelper.displayNotification(applicationContext, title!!, body!!, tag,foreground)
if (!foreground){
val random = Random().nextInt(1000).toString()
var intent = Intent(this, Activity_pendingorders::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK )
NotificationHelper.pendingIntent = PendingIntent.getActivity(this, Integer.valueOf(random) /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT)
NotificationHelper.pendingIntent!!.send(this,0,intent)
}
}
}
private inner class ForegroundCheckTask : AsyncTask<Context, Void, Boolean>() {
override fun doInBackground(vararg p0: Context?): Boolean {
val context = p0[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
}
}
}
我认为我错过了一些事情,请帮助我解决这个问题。