Open Specific Activity When E Push Notifications From Firebase
I have android application and tried to receive a push notifications. My problem when click the reserved notification open the main activity, I need when received push notificatio
Solution 1:
You can set a pending intent in to your notification. So first create a pending intent:
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Destination.class), 0);
Then add to your NotificationBuilder
, where you define the title, description, etc.. of the notification, the following parameter:
builder.setContentIntent(contentIntent)
UPDATE:
You have your NotificationCompatBuilder
already in your code.
Replace this:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.app_logo_a)
.setContentTitle(remoteMessage.getData().get("title"))
.setContentText(remoteMessage.getNotification().getBody());
with this:
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);
Solution 2:
You should consider using PendingIntent
to be able to start a specific activity when clicking on the notification. Add this to your code
Intentintent=newIntent(this, YourActivity.class);
PendingIntentcontentIntent= PendingIntent.getActivity(this, YOUT_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
You can change your code to this even though you are not implmented the handleDataMessage
well but this will allow you to start an activity when clicking on the notification
@OverridepublicvoidonMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message"));
//two lines adddedIntentintent=newIntent(this, YourActivity.class);
PendingIntentcontentIntent= PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builderbuilder=newNotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.app_logo_a)
.setContentTitle(remoteMessage.getData().get("title"))
.setContentText(remoteMessage.getNotification().getBody())
.setContentIntent(contentIntent);//added lineNotificationManagermanager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
try {
JSONObjectjson=newJSONObject(remoteMessage.getData().toString());
handleDataMessage(json);
} catch (Exception e) {
Log.e("Mo", "Exception: " + e.getMessage());
}
}
privatevoidhandleDataMessage(JSONObject json) {
if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
// app is in foreground, broadcast the push messageIntentpushNotification=newIntent(Config.PUSH_NOTIFICATION);
pushNotification.putExtra("message", message);
LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
NotificationUtilsnotificationUtils=newNotificationUtils(getApplicationContext());
notificationUtils.playNotificationSound();
}
}
Post a Comment for "Open Specific Activity When E Push Notifications From Firebase"