简体   繁体   English

Firebase云消息传递服务 - 即使应用程序被终止也会收到通知

[英]Firebase Cloud Messaging Service - Receive Notifications even when app is killed

If the Android app is killed deliberately by the user or Android, will the FirebaseMessagingService detect a notification when it receives a message? 如果Android应用被用户或Android故意杀死, FirebaseMessagingService会在收到消息时检测到通知吗?

I have killed my app and then sent a message from Firebase Console. 我已杀死我的应用,然后从Firebase控制台发送了一条消息。 I am unable to receive the notification. 我无法收到通知。 Is there a way I can receive a notification even if the app is killed. 有没有办法,即使应用程序被杀,我也可以收到通知。

public class YumigoBidMessagingService extends FirebaseMessagingService {

private static final String TAG = YumigoBidMessagingService.class.getSimpleName();

    /**
     * Called when message is received.
     *
     * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
     */
    // [START receive_message]

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // [START_EXCLUDE]
        // There are two types of messages data messages and notification messages. Data messages are handled
        // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
        // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
        // is in the foreground. When the app is in the background an automatically generated notification is displayed.
        // When the user taps on the notification they are returned to the app. Messages containing both notification
        // and data payloads are treated as notification messages. The Firebase console always sends notification
        // messages. 
        // [END_EXCLUDE]

        // TODO(developer): Handle FCM messages here.
        // Not getting messages here? See why this may be: 
        Log.d(TAG, "From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }
        sendNotification(remoteMessage.getData()+"");
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
    }
    /**
     * Create and show a simple notification containing the received FCM message.
     *
     * @param messageBody FCM message body received.
     */
    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, BiddingActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_ic_notification)
                .setContentTitle("Bid Received")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setPriority(Notification.PRIORITY_HIGH)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}

If the app is killed/force stopped, as mentioned in the answer from the link above: 如果应用程序被杀死/强制停止,如上面链接的答案中所述:

Force stop is a complete kill of the app -- all processes are killed, all services stopped , all notifications removed, all alarms removed, etc. 强制停止是对应用程序的完全终止 - 所有进程都被终止 ,所有服务都停止 ,所有通知都被删除,所有警报都被删除等。

Which would result for the app to don't receive any kind of notifications at all, as it was designed for Android since version 3.1, as stated in this answer : 这会导致应用程序根本没有收到任何类型的通知,因为它是从3.1版开始为Android设计的,如此答案中所述

Apps that are in the stopped state do not receive broadcast Intents. 处于停止状态的应用程序不接收广播Intents。

Stopped state is: 停止状态是:

when the app is initially installed (before the user runs something in the app) or after a Force Stop. 最初安装应用程序时(在用户在应用程序中运行某些内容之前)或强制停止之后。 You can find more about this here: http://developer.android.com/about/versions/android-3.1.html#launchcontrols 你可以在这里找到更多相关信息: http//developer.android.com/about/versions/android-3.1.html#launchcontrols

Just a retrieved a portion from my answer here . 仅有检索从我的答案的一部分在这里 Check out the thread, it may also be helpful for you. 查看主题,它也可能对您有所帮助。

There are two types of messages you can send using FCM ie notification or data message . 您可以使用FCM发送两种类型的消息,即通知数据消息

what you're sending is notification message , which if the app is in background is delivered to the system tray instead of your onMessageRecieved() callback. 您发送的是通知消息 ,如果应用程序处于后台,则会将其传送到系统托盘而不是onMessageRecieved()回调。

Try using data messages in which case you will always receive your notification in onMessageRecieved() callback, where you can handle as per you're need instead of depending on system tray. 尝试使用数据消息,在这种情况下,您将始终在onMessageRecieved()回调中收到通知,您可以根据需要处理,而不是依赖于系统托盘。

Shortly speaking, to wake your killed app, you should always send notification with data object to call your notification service class handler FirebaseMessagingService.onMessageReceived() in your application. 简而言之,要唤醒被杀的应用程序,您应始终使用data对象发送通知以在您的应用程序中调用通知服务类处理程序FirebaseMessagingService.onMessageReceived()

Also try sending it not from Firebase console , but to post it from somewhere else (eg online testing post service). 也可以尝试不是从Firebase控制台发送它,而是从其他地方发送它(例如在线测试后期服务)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使应用程序被杀死,whatsapp、messenger 和其他非常著名的消息传递应用程序如何接收推送通知? - How does whatsapp, messenger and other very famous messaging apps receive push notifications even when app is killed? 即使应用被杀死,如何继续接收地理围栏通知? - How to receive continuous geofence notifications even when app is killed? 应用程序因Firebase的Cloud Functions而被终止FCM时未收到通知 - Not receiving notifications when app is killed FCM with Cloud Functions for Firebase Firebase 云消息推送和应用内通知 - Firebase Cloud Messaging Push and In-app notifications 应用被杀死时接收PubNub推送通知 - Receive PubNub Push Notifications when app is killed 如何在 React Native 中接收来自 Firebase 云消息传递的通知 - How to receive notifications from Firebase Cloud Messaging in React Native Firebase Cloud Messaging Android接收静默推送通知 - Firebase Cloud Messaging Android receive silent push notifications 为什么即使消息服务进程不存在,Android应用仍然可以接收Firebase通知? - Why android app still can receive firebase notification even though the messaging service process not exists? Flutter 通知与 Firebase 云消息传递 - Flutter Notifications with Firebase Cloud Messaging 没有收到Firebase Cloud Messaging通知 - Not receiving Firebase Cloud Messaging Notifications
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM