Firebase Cloud Messaging Service - 即使应用程序被杀死也能收到通知

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

如果 Android 应用程序被用户故意杀死或 Android,FirebaseMessagingService 是否会在收到消息时检测到通知?

我终止了我的应用程序,然后从 Firebase 控制台发送了一条消息。我无法收到通知。有什么方法可以让我在应用程序被杀死的情况下收到通知。

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

您可以使用 FCM 发送两种类型的消息,即通知数据消息

您发送的是通知消息,如果应用程序在后台,它会传送到系统托盘而不是您的 onMessageRecieved() 回调。

尝试使用数据消息,在这种情况下,您将始终在 onMessageRecieved() 回调中收到通知,您可以根据需要进行处理,而不是依赖于系统托盘.

如果应用 killed/force 已停止,如上文 link 中的 answer 所述:

Force stop is a complete kill of the app -- all processes are killed, all services stopped, all notifications removed, all alarms removed, etc.

这将导致该应用根本不会收到任何类型的通知,因为它是为 Android 自版本 3.1 设计的,如此 answer 中所述:

Apps that are in the stopped state do not receive broadcast 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

刚刚从我的回答中检索了一部分。查看线程,它可能对您也有帮助。

简而言之,要唤醒被杀死的应用程序,您应该始终使用 data 对象 发送通知 以调用您的通知服务 class 处理程序 FirebaseMessagingService.onMessageReceived() 在您的应用程序中。

也尝试发送它不是从 Firebase 控制台,而是从其他地方post它(例如在线测试 post 服务)。