Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
97 views
in Technique[技术] by (71.8m points)

android - Firebase cloud message doesn't work when my app is closed


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To show notification, when your app is in the background you need to implement several things:

First - notification should be not 'notification' but 'data' type. Second - after receiving notification data in onMessageReceived of your service, you need to put notification with pending intent to the notification center. Third - add service to manifest.

Manifest:

<application>

   ... 

    <service android:name="com.xxx.xxx.xxx.xxx.xxx.xxxService"
        android:exported="false">

        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>

    </service>
    
   ...

</application>

Here is the code example written on Kotlin:

override fun onMessageReceived(remoteMessage: RemoteMessage) {
    super.onMessageReceived(remoteMessage)
    val notificationManager = context.getSystemService(NOTIFICATION_SERVICE) as? NotificationManager

    when(remoteMessage.data.isNotEmpty()) {
        true -> {
            with(remoteMessage) {

                val notificationModel = // build notification model form remoteMessage.data params

                val notification = buildNotification(context, notificationModel) // creating notification with pending intent
                val id: Int =  // notification id

                notificationManager?.notify(id, notification) // add notification with pending intent to notification center
            }
        }
    }
}

Creating notification with pending intent:

private fun buildNotification(context: Context, model: RemoteNotificationModel): Notification {
    val resultPendingIntent: PendingIntent? = TaskStackBuilder.create(context)
        .run {
            addNextIntentWithParentStack(
                Intent(context, YourActivity::class.java) // activity class, which will start after tap
                    .apply {
                        putExtra("key", "value") // pass model to activity
                        flags = Intent.FLAG_ACTIVITY_NEW_TASK or
                                Intent.FLAG_ACTIVITY_CLEAR_TASK
                    }
            )
            getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
        }

    return NotificationCompat.Builder(context, "default_channel_id")
        .apply {
            setContentTitle(model.title)
            setContentText(model.message)
            setSmallIcon(R.mipmap.ic_launcher)
            setAutoCancel(true)
            setContentIntent(resultPendingIntent)
        }.build()
}

More about pending intents: https://developer.android.com/training/notify-user/navigation


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...