简体   繁体   English

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

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

in AndroidManifest.xml在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>

My 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!!)
        }
    }

And show push:并显示推送:

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())
        })
    }

So:所以:

  1. when server send data or notification and application is run and foreground then method onMessageReceived success call and success show push notification (title and body) in the status bar (on top)当服务器发送数据通知并且应用程序运行并在前台运行时,方法onMessageReceived成功调用和成功在状态栏(顶部)中显示推送通知(标题和正文)

  2. when server send data or notification and application is run and minimize then method onMessageReceived success call and success show push notification in the status bar (on top)当服务器发送数据通知并且应用程序运行并最小化然后方法onMessageReceived成功调用和成功在状态栏中显示推送通知(在顶部)

  3. But when server send data or notification and application is NOT RUNNING then not call method onMessageReceived and also not show push notification on the status bar.但是,当服务器发送数据通知并且应用程序未运行时,则不要调用onMessageReceived方法,也不会在状态栏上显示推送通知。

But I need to show push notification on the status bar when app is not running.但是当应用程序未运行时,我需要在状态栏上显示推送通知。

I send data message to my android device by Python script:我通过 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 If I send message from Firebase console then success show push notification on status bar when app is not running. PS 如果我从 Firebase 控制台发送消息,那么当应用程序未运行时,成功在状态栏上显示推送通知。

Try to only send data payload (without notification payload) it should always call onMessageReceived .尝试只发送data负载(没有notification负载)它应该总是调用onMessageReceived Also try adding priority (my.js example):还可以尝试添加优先级(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.

相关问题 如果应用正在运行,则不会在状态栏中显示通知 - Not show notification in status bar if app is running 在推送通知中显示活动/弹出窗口,而不是状态栏中的消息 - On push notification show an activity/popup, instead of the message in the status bar 当应用程序不在 Android 上运行时,使用 React Native 在状态栏上显示通知图标 - Show notification icon on status bar with React Native when app is not running on Android 打开应用程序时,Firebase Cloud Messaging不显示通知栏 - Firebase Cloud Messaging not showing notification bar when app is open Android:当应用未运行或在后台运行时,将Firebase推送通知保存到磁盘 - Android: saving a Firebase push notification to disk when the app is not running or in background 应用销毁后如何使用Firebase显示推送通知 - How to show push notification using firebase when app is destroyed 仅在应用未运行android时显示推送通知 - Show push notification only when app is not running android 在应用程序运行时显示来自推送通知的弹出窗口 - Show popup from push notification when app is running 通过 api 显示应用程序在后台时收到的 Firebase 消息通知 - Show Notification of Firebase Message onReceived when App is in Background Through api 在应用运行时推送通知 - Push Notification when app is running
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM