Reputation:
I am using FCM to send custom notifications to the users, now i want that when an user clicks on the notification an activity should open.
Here is my code snippet from mainActivity.java:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("MyNotification", "MyNotification", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
And below is my Service class:
public class MyMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}
public void showNotification(String title, String message) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MyNotification")
.setContentTitle(title)
.setSmallIcon(R.drawable.noti)
.setAutoCancel(true)
.setContentText(message);
NotificationManagerCompat manager = NotificationManagerCompat.from(this);
manager.notify(999, builder.build());
}
}
Please tell me how to achieve this. Thanks in advance
Upvotes: 0
Views: 725
Reputation: 532
For click notification and go a specific activity, you should use Pending Intent. like below:
Intent activityIntent = new Intent(getApplicationContext(), SpecificActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Then make your Notification Builder like this
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MyNotification")
.setContentTitle(title)
.setSmallIcon(R.drawable.noti)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContentText(message);
So your finally showNotification()
method looks like this:
public void showNotification(String title, String message, PendingIntent pendingIntent) {
Intent activityIntent = new Intent(getApplicationContext(), SpecificActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MyNotification")
.setContentTitle(title)
.setSmallIcon(R.drawable.noti)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContentText(message);
NotificationManagerCompat manager = NotificationManagerCompat.from(this);
manager.notify(999, builder.build());
}
If anything you don't understand or have a query Comment here.
Upvotes: 1
Reputation: 574
// Create an Intent for the activity you want to start
Intent resultIntent = new Intent(this, ResultActivity.class);
// Create the TaskStackBuilder and add the intent, which inflates the back stack
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(resultIntent);
// Get the PendingIntent containing the entire back stack
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setContentIntent(resultPendingIntent);
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, builder.build());
for more information visit https://developer.android.com/training/notify-user/navigation
EDIT here is the code you want
public class MyMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Intent activityIntent = new Intent(getApplicationContext(), YourActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), pendingIntent);
}
public void showNotification(String title, String message, PendingIntent pendingIntent) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MyNotification")
.setContentTitle(title)
.setSmallIcon(R.drawable.noti)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContentText(message);
NotificationManagerCompat manager = NotificationManagerCompat.from(this);
manager.notify(999, builder.build());
}
}
Upvotes: 1