我正在处理FCM通知。当应用程序在后台时,我的通知工作正常,但当应用程序在前台时,我的通知无法接收。我几乎什么都试过了,但都不奏效。当应用程序处于前台时,不会收到通知。
manifest.xml:
<service
android:name=".Notification.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".Notification.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
代码(FirebaseMessagingService):
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage == null)
return;
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
handleNotification(remoteMessage.getNotification().getBody());
}
try {
JSONObject json = new JSONObject(remoteMessage.getData().toString());
handleDataMessage(json);
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
}
private void handleNotification(String message) {
if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
// app is in foreground, broadcast the push message
Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
pushNotification.putExtra("message", message);
LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
// play notification sound
NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
notificationUtils.playNotificationSound();
} else {
// If the app is in background, firebase itself handles the notification
}
}
private void handleDataMessage(JSONObject json) {
Log.e(TAG, "push json: " + json.toString());
try {
JSONObject data = json.getJSONObject("data");
String title = data.getString("title");
String message = data.getString("body");
boolean isBackground = data.getBoolean("is_background");
String imageUrl = data.getString("image");
String timestamp = data.getString("timestamp");
JSONObject payload = data.getJSONObject("payload");
Log.e(TAG, "title: " + title);
Log.e(TAG, "message: " + message);
Log.e(TAG, "isBackground: " + isBackground);
Log.e(TAG, "payload: " + payload.toString());
Log.e(TAG, "imageUrl: " + imageUrl);
Log.e(TAG, "timestamp: " + timestamp);
if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
// app is in foreground, broadcast the push message
Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
pushNotification.putExtra("message", message);
LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
// play notification sound
NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
notificationUtils.showNotificationMessage("iGrab", message, timestamp, pushNotification);
notificationUtils.playNotificationSound();
} else {
// app is in background, show the notification in notification tray
Intent resultIntent = new Intent(getApplicationContext(), Splash.class);
resultIntent.putExtra("message", message);
// check for image attachment
if (TextUtils.isEmpty(imageUrl)) {
showNotificationMessage(getApplicationContext(), title, message, timestamp, resultIntent);
} else {
// image is present, show notification with image
showNotificationMessageWithBigImage(getApplicationContext(), title, message, timestamp, resultIntent, imageUrl);
}
}
} catch (JSONException e) {
Log.e(TAG, "Json Exception: " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
当应用程序在前台时不接收推送通知,但当应用程序在后台时接收我已遵循来自https://medium . com/@ ankushaggarwal/GCM-setup-for-Android-push-notifications-656 cf DD 8 adbd的FCM教程
我知道我可以设置click_action,但这只是为了定制哪些活动将在通知点击时启动,我需要一些将在通知收到时自动启动的东西。 我尝试了quick start messaging firebase示例,其中有一个onMessageReceived()方法,但它仅在应用程序处于前台时才起作用。是否有一些东西将执行同时应用程序在后台?GCM可以通过直接从接收到通知时调用的广播接收器启动活动意图来完成我
有时当应用程序收到通知时,它不会发出声音。下面是代码段。
我正在尝试构建一个简单的应用程序,可以从FCM(Firebase云消息传递系统)接收通知。我能够生成令牌并将其显示在文本视图中。另外,我还编写了在“FirebaseMessagingService”类“中使用log.d()在LOG上打印通知消息的逻辑,该类从”FirebaseMessagingService“扩展而来。但是,一旦我从FCM控制台发送消息,应用程序就会崩溃,并且在Android显示器
我在Android上尝试了以下内容: 从Firebase控制台发送通知:只有当应用程序位于后台时,我才能在系统托盘中看到通知。 将post请求发送到(如本文所述),同时使用和Paylods:同样,我只能在应用程序位于后台时在系统托盘中看到通知。 该文件提到: 我用的是Android和Cordova/Ionic。
当我的应用程序前台服务被终止时,不会收到FCM通知,更清楚的是,我正试图让我的应用程序始终在后台运行,我使用前台服务来实现这一点,有时应用程序未被ANDROID系统终止,前台通知会出现15个多小时,但其他情况下,当我的前台通知被终止,并且我发送FCM通知时,它在1小时或更短时间内被终止,而我的设备没有收到FCM通知!!这里有什么问题!我怎样才能解决它!