我有安卓应用程序,并尝试接收推送通知。单击保留通知打开主活动时我的问题,我需要在收到来自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);
}
}
您可以在通知中设置挂起的意图。因此,首先创建一个挂起的意图:
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Destination.class), 0);
然后将以下参数添加到您的NotificationBuilder
,您可以在其中定义通知的标题、说明等:
builder.setContentIntent(contentIntent)
更新:
您的代码中已经有了NotificationCompatBuilder
。
替换此内容:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.app_logo_a)
.setContentTitle(remoteMessage.getData().get("title"))
.setContentText(remoteMessage.getNotification().getBody());
有了这个:
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Destination.class), PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.app_logo_a)
.setContentTitle(remoteMessage.getData().get("title"))
.setContentText(remoteMessage.getNotification().getBody())
.setContentIntent(contentIntent);
您应该考虑使用 PendingIntent
以便在单击通知时能够启动特定活动。将其添加到您的代码中
Intent intent = new Intent(this, YourActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, YOUT_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
即使您没有很好地了解handleDataMessage
,您也可以将代码更改为此代码,但这将允许您在单击通知时启动活动
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message"));
//two lines addded
Intent intent = new Intent(this, YourActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.app_logo_a)
.setContentTitle(remoteMessage.getData().get("title"))
.setContentText(remoteMessage.getNotification().getBody())
.setContentIntent(contentIntent);//added line
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());
}
}
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();
}
}