Android 从firebase发送电子推送通知时打开特定活动
我有android应用程序,并尝试接收推送通知。 单击保留通知打开主活动时出现的问题, 我需要在收到来自firebase的推送通知时直接打开特定活动。这是我的密码Android 从firebase发送电子推送通知时打开特定活动,android,firebase,push-notification,firebase-cloud-messaging,Android,Firebase,Push Notification,Firebase Cloud Messaging,我有android应用程序,并尝试接收推送通知。 单击保留通知打开主活动时出现的问题, 我需要在收到来自firebase的推送通知时直接打开特定活动。这是我的密码 public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage remoteMessage) { super
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);
然后添加到您的通知生成器,在其中定义标题、说明等。。通知中的以下参数:
builder.setContentIntent(contentIntent)
更新:
您的代码中已经有了notificationcompatingbuilder
替换此项:
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);
你应该考虑使用<代码> PixIdtudio可以在点击通知时启动一个特定的活动。将此添加到您的代码中
Intent intent = new Intent(this, YourActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, YOUT_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
即使未执行HandleData消息
也可以将代码更改为该消息,但这将允许您在单击通知时启动活动
@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();
}
}
检查一下这个。。当您收到推送通知或当用户在UI中“单击”通知时,您是否尝试启动活动?在哪里??加上这个?!!哪里加上这个@莫亚耶达莱塞啊。。打字错误。我修好了。您能再试一次吗?@MoayedAlayseh您是否设置了目标活动?PendingEvent contentIntent=PendingEvent.getActivity(此,0,新意图(此,通知页面类),0);NotificationCompat.Builder Builder=新建NotificationCompat.Builder(this).setSmallIcon(R.mipmap.app_logo_a).setContentTitle(remoteMessage.getData().get(“title”).setContentText(remoteMessage.getNotification().getBody()).setContentIntent(contentIntent);NotificationManager=(NotificationManager)getSystemService(通知服务);manager.notify(0,builder.build());这是我的代码,当点击进入通知时,应用程序将打开第一个活动!!!