Reputation: 41
I have an app which displays notifications generated from the backend, each and every notification contains different data and when clicked upon should display the appropriate content on the notification page.
For example: Say i get 3 notifications one has the data RED with id 1, then i get another which has BLUE with id 2 and another with data GREEN with id 3, so whenever i click on any notification say RED, BLUE or GREEN it always opens the notification page for RED
@Override
public void onReceive(Context context, Bundle bundle) {
ctx = context;
model = new Model();
String subtitle = bundle.getString("subtitle");
String title = bundle.getString("title");
String body = bundle.getString("body");
String eventJsonString = bundle.getString("extraData");
JSONObject extraObj = null;
try {
extraObj = new JSONObject(eventJsonString);
} catch (JSONException e) {
e.printStackTrace();
}
String Id = null;
try {
deviceId = extraObj.getString("Id");
} catch (JSONException e) {
e.printStackTrace();
}
if( Id == null){
Log.d("Notification","ready notification "+bundle.get("Id"));
Intent result = new Intent();
result.setAction(ServiceConstants.NOTIFICATION_RECEIVED);
LocalBroadcastManager.getInstance(context).sendBroadcast(result);
}
else{
isNotification = true;
try {
model.setId(Integer.parseInt(extraObj.getString("Id")));
} catch (Exception e) {
e.printStackTrace();
}
//Type is not available with notification bundle
try {
model.setType(extraObj.getString("Type"));
} catch (Exception e) {
e.printStackTrace();
model.setType("NA");
}
}
sendNotification(title, subtitle, body, context);
}
private void sendNotification(String title, String subtitle, String body, Context context) {
Intent intent = null;
if(isNotification){
intent = new Intent(ctx, NotificationActivity.class);
intent.putExtra("model", model);
}
else{
intent = new Intent(ctx, MyActivity.class);
}
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mNotificationManager = (NotificationManager)
ctx.getSystemService(NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
String id = "com.example.testnotification";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(id, title, NotificationManager.IMPORTANCE_DEFAULT);
mChannel.enableLights(true);
mChannel.enableVibration(true);
mChannel.setLightColor(Color.GREEN);
mNotificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(ctx)
.setSmallIcon(R.drawable.launcher_icon)
.setContentTitle(title)
.setChannelId(id)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(subtitle +"\n" + body))
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentText(body);
mBuilder.setContentIntent(contentIntent);
mBuilder.setAutoCancel(true);
int random = (int) (Math.random() * 50 + 1);
mNotificationManager.notify(random, mBuilder.build());
}
Upvotes: 2
Views: 1241
Reputation: 357
Pass unique id here
PendingIntent contentIntent = PendingIntent.getActivity(ctx,id,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
Upvotes: 4