簡體   English   中英

當應用程序未運行時,不在狀態欄上顯示推送通知(Firebase 雲消息)

[英]Not show push notification (Firebase Cloud Message) on status bar when app is not running

在AndroidManifest.xml

 <service
        

android:name=".CustomFirebaseInstanceIDService"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>


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

我的服務:

import android.util.Log
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import org.tokend.template.BuildConfig

class CustomFirebaseMessagingService : FirebaseMessagingService() {
    override fun onMessageReceived(remoteMessage: RemoteMessage?) {
        if (remoteMessage?.data?.isNotEmpty()!!) {
            val payloadData: Map<String, String> = remoteMessage.data
            PushNotificationService.showNotification(applicationContext, payloadData["title"]!!, payloadData["body"]!!)
        }

        // Check if message contains a notification payload.
        remoteMessage.notification?.let {
            val notificationTitle : String? = it.title
            val notificationBody: String? = it.body
            PushNotificationService.showNotification(applicationContext, notificationTitle!!, notificationBody!!)
        }
    }

並顯示推送:

object PushNotificationService {
    val TAG = PushNotificationService::class.java.name
    val CHANNEL_ID = "channelId"
    val NOTIFICATON_ID = 1

    fun showNotification(context: Context, title: String, body: String) {
        val intent = Intent(context, SignInActivity::class.java).apply {
            this.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        }
        val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, intent, 0)

        val builder = NotificationCompat.Builder(context, CHANNEL_ID)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
                        R.mipmap.ic_launcher))
                .setContentTitle(title)
                .setStyle(NotificationCompat.BigTextStyle().bigText(body))
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                // Set the intent that will fire when the user taps the notification
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)

        // Show the notification
        with(NotificationManagerCompat.from(context), {
            // NOTIFICATON_ID is a unique int for each notification that you must define
            this.notify(NOTIFICATON_ID, builder.build())
        })
    }

所以:

  1. 當服務器發送數據通知並且應用程序運行並在前台運行時,方法onMessageReceived成功調用和成功在狀態欄(頂部)中顯示推送通知(標題和正文)

  2. 當服務器發送數據通知並且應用程序運行並最小化然后方法onMessageReceived成功調用和成功在狀態欄中顯示推送通知(在頂部)

  3. 但是,當服務器發送數據通知並且應用程序未運行時,則不要調用onMessageReceived方法,也不會在狀態欄上顯示推送通知。

但是當應用程序未運行時,我需要在狀態欄上顯示推送通知。

我通過 Python 腳本向我的 android 設備發送數據消息:

import firebase_admin, sys
from firebase_admin import credentials, messaging
message = messaging.Message(
    data={
        "title": "Test data",
        "body": "Body of long test text of data test long body message for type data"
    },
    token=sys.argv[1],
)

response = messaging.send(message)

PS 如果我從 Firebase 控制台發送消息,那么當應用程序未運行時,成功在狀態欄上顯示推送通知。

嘗試只發送data負載(沒有notification負載)它應該總是調用onMessageReceived 還可以嘗試添加優先級(my.js 示例):

const  payload = {
        data: {
            title: 'finalTitle',
            body: 'message',
        },
        android:{
            priority: 'high'
            },
        topic: channel
    };

    admin.messaging().send(payload)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM