Firebase 云消息服务 - 即使应用程序已关闭也能接收通知

3
如果安卓应用程序被用户或系统意外关闭,那么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());
    }
}

请使用您的设备令牌在单个设备上进行尝试。 - Aditya Vyas-Lakhan
我已经尝试过了,才发布这个信息。当我杀死我的应用程序时,我没有收到任何通知。但是,当我的应用程序在系统托盘中运行时,我就能收到通知。 - androider
上传您的 FCM 服务代码。 - farhan patel
@mayank,你能让它工作吗?(手动应用程序后接收通知)因为我也遇到了同样的问题,没有头绪。 - DEEPANKUR SADANA
按照这个答案来获取精确的解决方案 如何在应用程序被关闭时接收FCM消息? - Siddharth Shakya
显示剩余2条评论
3个回答

2
如果应用程序被杀死/强制停止,如上述链接中的答案所述:
强制停止是完全终止应用程序——所有进程都被终止,所有服务都停止,所有通知都被删除,所有警报都被删除等。
这将导致应用程序根本不会收到任何通知,因为它从Android 3.1版本开始就被设计为这样,如答案中所述: 处于停止状态的应用程序不接收广播意图。 停止状态是: 当应用程序最初安装时(在用户运行应用程序之前)或 强制停止后。 您可以在此处了解更多信息:http://developer.android.com/about/versions/android-3.1.html#launchcontrols 这只是我在这里回答的��部分。查看该线程,它也可能对您有帮助。

2
那么我能实现这个吗,因为我需要设备接收通知。我的意思是WhatsApp、Facebook和其他新闻提要应用程序正在这样做,所以应该有某种方式。 - androider
我不是很确定,因为我以前没有尝试过。但是,我记得在某个地方读到过使用START_STICKY会有帮助。在社区中搜索一下。在谷歌上搜索一下。我相信这是一个常见的问题。 :) - AL.

1
有两种类型的消息可以使用 FCM 发送,即“通知”或“数据消息”。
您发送的是“通知消息”,如果应用程序在后台,则会将其传递到系统托盘而不是您的 onMessageRecieved() 回调中。
尝试使用“数据消息”,这样您将始终在 onMessageRecieved() 回调中收到通知,您可以根据需要进行处理,而不是依赖于系统托盘。

4
我已经关闭了我的应用程序,没有任何实例在运行。onMessageReceived功能无法使用,该应用程序没有在后台运行,它已经完全关闭了。 - androider
1
正如明确提到的那样,即使应用程序完全被关闭,当发送数据消息时,MessagingService也会重新启动。 - rpattabi
简单且有效的解决方案!即使应用程序已关闭,也可以正常工作。也许在重新启动设备之后它将无法工作,直到应用程序被启动一次 - 但在这种情况下,添加一个带有´ACTION_BOOT_COMPLETED´的´BroadcastReceiver´可能会有所帮助。 - coyer
1
@rpattabi,您说的这个在哪里清楚地提到了呢?我正在发送数据消息,但它并不能工作...请提供一份支持您说法的参考资料。 - pjdupreez
1
@rpattabi 这与我的情况不同。当我将应用程序放到后台并发送数据消息时,FirebaseMessagingService中的onMessageReceived()将接收到该消息(有趣的是,每个消息都会创建然后销毁FirebaseMessagingService)。当我通过从打开的应用程序列表中滑动来关闭应用程序时,在onMessageReceived()中不再接收到消息。我已经观察到这种行为在多个来自不同制造商的设备上。 - aaronmarino
显示剩余2条评论

0
简单来说,要唤醒您已关闭的应用程序,您应该始终发送带有data对象的通知,以调用您的通知服务类处理程序FirebaseMessagingService.onMessageReceived()在您的应用程序中。
同时尝试从其他地方(例如,在线测试发布服务)而不是从Firebase控制台发送它。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接