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

时间:2016-09-08 09:43:13

标签: android firebase firebase-cloud-messaging firebase-notifications

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

3 个答案:

答案 0 :(得分:2)

如果应用被杀死/强行停止,请参阅上面链接中的answer

  

强制停止是对应用程序的完全终止 - 所有进程都被终止,所有服务已停止,所有通知都被删除,所有警报都被删除等等。

这会导致应用无法接收任何类型的通知,因为它是自3.1版以来为Android设计的,如answer所述:

  

处于停止状态的应用不会收到广播意图。

     

停止状态是:

     

最初安装应用程序时(在用户在应用程序中运行某些内容之前)或   在一次强制停止之后。   您可以在此处找到有关此内容的更多信息:http://developer.android.com/about/versions/android-3.1.html#launchcontrols

只是从我的回答here中检索了一部分。查看帖子,它也可能对你有帮助。

答案 1 :(得分:1)

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

您发送的内容是通知消息,如果应用程序处于后台,则会将其发送到系统托盘而不是onMessageRecieved()回调。

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

答案 2 :(得分:0)

简而言之,要唤醒被杀的应用,您应该始终发送带有data对象的通知来调用您的通知服务类处理程序 FirebaseMessagingService.onMessageReceived()在你的申请中。

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