[英]How does whatsapp, messenger and other very famous messaging apps receive push notifications even when app is killed?
[英]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());
}
}
如果應用程序被殺死/強制停止,如上面鏈接的答案中所述:
強制停止是對應用程序的完全終止 - 所有進程都被終止 ,所有服務都停止 ,所有通知都被刪除,所有警報都被刪除等。
這會導致應用程序根本沒有收到任何類型的通知,因為它是從3.1版開始為Android設計的,如此答案中所述 :
處於停止狀態的應用程序不接收廣播Intents。
停止狀態是:
最初安裝應用程序時(在用戶在應用程序中運行某些內容之前)或強制停止之后。 你可以在這里找到更多相關信息: http : //developer.android.com/about/versions/android-3.1.html#launchcontrols
僅有檢索從我的答案的一部分在這里 。 查看主題,它也可能對您有所幫助。
您可以使用FCM發送兩種類型的消息,即通知或數據消息 。
您發送的是通知消息 ,如果應用程序處於后台,則會將其傳送到系統托盤而不是onMessageRecieved()回調。
嘗試使用數據消息,在這種情況下,您將始終在onMessageRecieved()回調中收到通知,您可以根據需要處理,而不是依賴於系統托盤。
簡而言之,要喚醒被殺的應用程序,您應始終使用data
對象發送通知,以在您的應用程序中調用通知服務類處理程序FirebaseMessagingService.onMessageReceived() 。
也可以嘗試不是從Firebase控制台發送它,而是從其他地方發送它(例如在線測試后期服務)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.