【发布时间】:2017-06-14 12:10:17
【问题描述】:
我有 android 应用程序并尝试接收推送通知。 单击保留通知打开主要活动时我的问题, 当收到来自 firebase 的推送通知时,我需要直接打开特定活动。这是我的代码
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message"));
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.app_logo_a)
.setContentTitle(remoteMessage.getData().get("title"))
.setContentText(remoteMessage.getNotification().getBody());
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
try {
JSONObject json = new JSONObject(remoteMessage.getData().toString());
handleDataMessage(json);
} catch (Exception e) {
Log.e("Mo", "Exception: " + e.getMessage());
}
Intent resultIntent = new Intent(getApplicationContext(), Notification_Page.class);
startActivity(resultIntent);
}
private void handleDataMessage(JSONObject json) {
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);
NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
notificationUtils.playNotificationSound();
Intent resultIntent = new Intent(getApplicationContext(), Notification_Page.class);
startActivity(resultIntent);
} else {
Intent resultIntent = new Intent(getApplicationContext(), Notification_Page.class);
startActivity(resultIntent);
}
}
【问题讨论】:
-
您是在收到推送通知还是当用户在 UI 中“点击”通知时尝试启动活动?
标签: android firebase push-notification firebase-cloud-messaging