I want to start a pendingIntent from a notification i send from App A = Admin to App B = users. I want to know if it possible to open a pendingIntent immediately a user or admin receives a notification(from each end)?
My App crashes when i try to open a notification sent from admin to user app and vice versa but I am using the same method below in both apps. What am i doing wrong?
NB : it is just a chat and suggestion app and it works like any other chat application.
// sending notification to devices with versions below Oreo(android 8.0)
private void sendNotification(RemoteMessage remoteMessage) {
String user = remoteMessage.getData().get("user");
String icon = remoteMessage.getData().get("icon");
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
RemoteMessage.Notification notification = remoteMessage.getNotification();
// creating a pendingIntent for notification
int j = Integer.parseInt(user.replaceAll("[\\D]",""));
Intent intent = new Intent(this,MessageActivity.class);
Bundle bundle = new Bundle();
bundle.putString("userid",user);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,j,intent,PendingIntent.FLAG_ONE_SHOT);
// notification sound uri
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// creating an instance of Notification builder and setting its properties
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.app_logo_round)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(false)
.setSound(defaultSound)
.setVibrate(new long[]{1000,1000,1000})
.setWhen(System.currentTimeMillis());
.setContentIntent(pendingIntent);
// creating an instance of Notification Manager
NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
int i = 0;
if(j > 0){
i = j;
}
// starting the notification
nm.notify(i,builder.build());
}
What I want to achieve is to be able to open app from notification if an admin sends a message which comes with a notification to the user and vice versa.