gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:47:36 25 4
gpt4 key购买 nike

如果 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) orafter 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com