【发布时间】:2018-01-09 04:50:12
【问题描述】:
我正在按照this 教程在我的应用中实现 Firebase 推送通知 功能。
但我发现了一件事,如果应用程序位于 foreground 中,那么只有我在 Toast 和 TextView 中获取(显示)消息。
另一方面,如果应用程序在 background 中点击,我也不会没有收到消息显示在 TextView 和 Toast 中。
而我想在 Toast 和 TextView 两种情况下都显示消息 (Either App is in Foreground or Background)。
注意:我正在从 Firebase 控制台本身推送消息。
有可能吗?
MyFirebaseMessagingService.java
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
}
}
MainActivity.java
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// checking for type intent filter
if (intent.getAction().equals(Config.REGISTRATION_COMPLETE)) {
// gcm successfully registered
// now subscribe to `global` topic to receive app wide notifications
FirebaseMessaging.getInstance().subscribeToTopic(Config.TOPIC_GLOBAL);
displayFirebaseRegId();
} else if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) {
// new push notification is received
String message = intent.getStringExtra("message");
Toast.makeText(getApplicationContext(), "Push notification: " + message, Toast.LENGTH_LONG).show();
txtMessage.setText(message);
}
}
};
【问题讨论】:
-
如果您想在后台和前台处理通知,那么您可能需要查看此答案Here 并使用邮递员将通知发布到应用程序,您可能还需要查看此答案@ 987654323@
标签: android firebase push-notification android-service firebase-cloud-messaging