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

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

最佳答案

如果应用程序被终止/强制停止,如 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

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

关于android - Firebase Cloud Messaging Service - 即使应用程序被杀死也能收到通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39387602/

相关文章:

找不到android对命令(无线调试)

ios - Swift Firebase 按距离排序

firebase - 收到来自 Firebase 云消息传递的通知,但未显示在云消息传递报告中

android - 手动启动 Firebase 服务

android - 如何从我的 Firebase 数据库访问特定值?

android - Firebase Cloud Messaging 文档中的 "app server in my own environment"是什么意思?

android - 测试版应用程序的谷歌应用程序许可?

java - 将 ArrayList 的一部分分配给字符串

java - 适用于 Java 的 Google Http 客户端 : Ignore java. security.cert.CertPathValidatorException:未找到证书路径的信任 anchor

node.js - 针对电话验证用户的 Firebase 重新身份验证和电子邮件链接