【问题标题】:Not show push notification (Firebase Cloud Message) on status bar when app is not running应用未运行时,不在状态栏上显示推送通知(Firebase 云消息)
【发布时间】:2020-10-21 21:22:49
【问题描述】:

在 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. 但是当服务器发送 datanotification 并且应用程序未运行时 不调用 方法 onMessageReceived 并且也不显示 在状态栏上推送通知。

但我需要在应用未运行时在状态栏上显示推送通知。

我通过 Python 脚本向我的安卓设备发送数据消息:

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 控制台发送消息然后成功显示应用未运行时状态栏上的推送通知。

【问题讨论】:

    标签: android firebase-cloud-messaging


    【解决方案1】:

    尝试只发送data 有效载荷(没有notification 有效载荷)它应该始终调用onMessageReceived。还可以尝试添加优先级(我的 .js 示例):

    const  payload = {
            data: {
                title: 'finalTitle',
                body: 'message',
            },
            android:{
                priority: 'high'
                },
            topic: channel
        };
    
        admin.messaging().send(payload)
    

    【讨论】:

    • 没有帮助。仅发送数据时不调用 onMessageReceived
    • 我已经用示例有效负载更新了我的答案。你的清单在我看来已经很好了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-08
    • 2017-08-24
    • 2020-12-31
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多