我在我的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. FCM通知问题
- 2. FCM通知问题 - swift3
- 3. Android:传递数据的背景中的FCM推送通知
- 4. Android FCM通知包问题(排队通知)
- 5. Firebase背景通知图标问题
- 6. IONIC android FCM通知
- 7. android背景问题
- 8. 背景通知
- 9. Fcm通知push android/IOS nodejs
- 10. Swift3无法接收背景FCM推送通知
- 11. Android - Listview背景问题
- 12. iPhone背景通知
- 13. 从背景通知
- 14. Android通知问题
- 15. Android通知问题
- 16. 背景问题
- 17. FCM Web - 相同的前景和背景通知(在前台重复使用后台通知)
- 18. 在GCM通知中设置Android Wear通知背景图片
- 19. 背景通知+ Windows 8
- 20. FCM抬头通知
- 21. 从fcm获取旧通知Android
- 22. Android FCM显示触发服务通知
- 23. 如何在Android上分开FCM通知?
- 24. 发送通知到Android使用FCM
- 25. MobileFirst 7.1.0 Android FCM推送通知
- 26. Android通知FCM vs简单拉
- 27. Android的FCM - 禁用/启用通知
- 28. 使用FCM向Android推送通知(c#)
- 29. 根据主题发送fcm通知
- 30. 将通知推送到FCM主题
请出示你的代码 –
检查。同样我已经实现https://www.simplifiedcoding.net/firebase-cloud-messaging-tutorial-android/ –
它是你的代码还是你指向我们一个你提到的在线文章? –