Android 应用程序未运行时,不在状态栏上显示推送通知(Firebase云消息)

Android 应用程序未运行时,不在状态栏上显示推送通知(Firebase云消息),android,firebase-cloud-messaging,Android,Firebase Cloud Messaging,在AndroidManifest.xml中 <service android:name=".CustomFirebaseInstanceIDService" android:exported="true"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_

在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>
因此:

  • 当服务器发送数据通知且应用程序运行且前台时,方法
    onMessageReceived
    成功调用和成功在状态栏(顶部)中显示推送通知(标题和正文)

  • 当服务器发送数据通知且应用程序运行且最小化时,方法
    onMessageReceived
    成功调用和成功在状态栏(顶部)中显示推送通知

  • 但当服务器发送数据通知且应用程序未运行时,则不会调用方法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)
    

    另外,如果我从Firebase控制台发送消息,则当应用程序未运行时,成功显示状态栏上的推送通知。

    尝试仅发送
    数据
    有效负载(无
    通知
    有效负载),它应始终调用
    onMessageReceived
    。还可以尝试添加优先级(my.js示例):


    没有帮助。仅发送数据时不调用MessageReceived我已使用示例负载更新了答案。我觉得你的舱单已经很好了。
    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())
            })
        }
    
    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)
    
    const  payload = {
            data: {
                title: 'finalTitle',
                body: 'message',
            },
            android:{
                priority: 'high'
                },
            topic: channel
        };
    
        admin.messaging().send(payload)