我在我的android項目中使用FCM,並且在應用運行時工作正常。但是當應用程序被殺或關閉onReceiveMessage沒有被調用。我曾嘗試通過僅使用數據播放器發送消息,但它仍然無法正常工作。有沒有任何解決方案。在此先感謝FCM背景通知問題android
1
A
回答
0
試試這個代碼它會解決您的問題。在MainActivity通過getIntent()
MyFirebaseMessagingService.java
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
/**
* 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) {
// If the application is in the foreground handle both data and notification messages here.
// 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.
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Title: " + remoteMessage.getNotification().getTitle());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
sendNotification("", "");
}
// [END receive_message]
/**
* Create and show a simple notification containing the received FCM message.
*
* @param messageBody FCM message body received.
*/
private void sendNotification(String xyz, String abc) {
Intent intent = new Intent(this, MainActivity.class);
Bundle b = new Bundle();
b.putString("key", xyz);
b.putString("key", abc);
intent.putExtras(b);
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)
.setContentTitle("title")
.setContentText("body")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
boolean useSilhouette = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
if (useSilhouette) {
try {
notificationBuilder.setSmallIcon(R.mipmap.ic_sillate);
} catch (Exception e) {
notificationBuilder.setSmallIcon(R.mipmap.ic_sillate);
}
} else {
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
}
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//Random id number for notification
Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
notificationManager.notify(m/* ID of notification */, notificationBuilder.build());
}
}
獲取值,做你想做的事;)
+0
我有以同樣的方式實施它在應用程序關閉時不起作用。 –
+0
編輯你的問題併發布你的代碼。 –
相關問題
- 1. Android FCM通知問題
- 2. FCM通知問題
- 3. FCM通知問題 - swift3
- 4. Android:傳遞數據的背景中的FCM推送通知
- 5. Android FCM通知包問題(排隊通知)
- 6. IONIC android FCM通知
- 7. FCM通知爲Android
- 8. Firebase背景通知圖標問題
- 9. android背景問題
- 10. FCM通知標題仍爲「FCM消息」
- 11. 背景通知
- 12. Fcm通知push android/IOS nodejs
- 13. Android推送通知,fcm
- 14. Swift3無法接收背景FCM推送通知
- 15. Android按鈕背景問題
- 16. Android - Listview背景問題
- 17. android背景圖像問題
- 18. 圖像背景問題android
- 19. 從背景通知
- 20. iPhone背景通知
- 21. android通知問題
- 22. Android通知問題
- 23. Android通知問題
- 24. Android:通知問題
- 25. Firebase Android前景/背景問題
- 26. Android的FCM圖標問題
- 27. 背景問題
- 28. 背景問題
- 29. FCM Web - 相同的前景和背景通知(在前臺重複使用後臺通知)
- 30. 在GCM通知中設置Android Wear通知背景圖片
請出示你的代碼 –
檢查。同樣我已經實現https://www.simplifiedcoding.net/firebase-cloud-messaging-tutorial-android/ –
它是你的代碼還是你指向我們一個你提到的在線文章? –