I am using GCM Notification in my app.I parse the data in "onMessageReceived" method where i get some unique "URL" corresponding to GCM Message.When I receive single notification Every thing is fine,But when i receive multiple notifications problem occurs when i tap any notification last received notification url loads.
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
String country = data.getString("country");
String url = data.getString("url");
String description = data.getString("description");
String id = data.getString("id");
}
I get url from GCM Notification
Intent intent = new Intent(this, Detailview.class).putExtra("url", url);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(Mainscreen.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent= stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_ONE_SHOT );
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
contentView.setImageViewResource(R.id.image, R.drawable.ic_stat_ic_notification);
contentView.setTextViewText(R.id.title, message);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setAutoCancel(true)
.setContent(contentView)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Integer.parseInt(id)
/*ID of notification */
, notificationBuilder.build());
so when i tap the notification i am able to launch only last url How i can solve this problem?
use
PendingIntent.FLAG_UPDATE_CURRENT
when creating PendingIntent.
Also before passing intent to PendingIntent set its action different everytime. e.g.:-
intent.setAction(Long.toString(System.currentTimeMillis()));
Just add time of arrive as a differntiator to each notification
PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Related
i have created a cloud function which sends data with the notifications. Now i want to open always the specific activity. Unfortunately its opening always the MainActivity. Here is my Code of my Firebase Service:
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> dataSet = remoteMessage.getData();
if (!dataSet.get("kind").equals("0")) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.baseline_send_24)
.setContentTitle(remoteMessage.getData().get("title"))
.setContentText(remoteMessage.getData().get("body"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}else{
Intent intent = new Intent(this, SettingsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.baseline_send_24)
.setContentTitle(remoteMessage.getData().get("title"))
.setContentText(remoteMessage.getData().get("body"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
I dont know if i have to define it somewhere else. Thanks
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction("ACTION.HOME_ACTION");
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
and set this pendingintent in notification
.setContentIntent(pendingIntent)
Incase you are using Firebase for your push notifications. If you use the 'notification' type payload, while sending from server, Firebase manages the display of notification for your app without giving a callback and by default will always open the launcher activity of your app.
You can send extra data from the server side, access that data in your launcher activity getIntent methods and redirect to some other activity as needed
I have made a chat app in firebase and users are being notified when a new message comes by the below codes.
private void sendNotification(String title, String message, String receiver, String receiverUid, String firebaseToken) {
Intent intent = new Intent(this, ChatActivity.class);
intent.putExtra("name",title);
intent.putExtra(Const.ARG_RECEIVER, receiver);
intent.putExtra(Const.ARG_RECEIVER_UID, receiverUid);
intent.putExtra(Const.ARG_FIREBASE_TOKEN, firebaseToken);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
char[] ch = firebaseToken.substring(0,3).toCharArray();
String a = "";
for (char aCh : ch) {
a = a + Integer.toHexString(aCh);
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, Integer.parseInt(a), intent,
PendingIntent.FLAG_ONE_SHOT);
// Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_noti)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
// .setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Integer.parseInt(a), notificationBuilder.build());
}
I want to receive different notification when a different user sends a message but for the same user, the notification should be updated. I tried to set unique id by taking the Unicode of first 3 letters of Firebase token of the user but then also it is showing only one notification. If I chat with 2 people the user with whom I have started chat first shows notification but not the second one.
Please have a look and help. Thanks
Just change FLAG_ONE_SHOT to FLAG_UPDATE_CURRENT.
FLAG_ONE_SHOT :
Flag indicating that this PendingIntent can be used only once.
FLAG_UPDATE_CURRENT :
Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent.
Nothing happens on clicking Accept or Reject button of the head-ups notification.
But when the head-up notification disappear and from clicking Accept and Reject from the notification panel is working.
Testing on Android 5.1.0.
Intent acceptIntent = new Intent(this, NotificationReceiver.class);
acceptIntent.setAction("com.android.test.Accept");
PendingIntent acceptPendingIntent = PendingIntent.getBroadcast(TestApplication.getAppContext(), 12345, acceptIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Intent rejectIntent = new Intent(this, NotificationReceiver.class);
rejectIntent.setAction("com.android.test.Reject");
PendingIntent rejectPendingIntent = PendingIntent.getBroadcast(TestApplication.getAppContext(), 12345, rejectIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.fundu);
builder.setContentTitle("Test Notification");
builder.setContentText("Hello");
builder.setAutoCancel(true);
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
builder.setCategory(NotificationCompat.CATEGORY_SERVICE);
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
builder.setPriority(NotificationCompat.PRIORITY_MAX);
builder.addAction(R.drawable.ic_check_icon, "Accept", acceptPendingIntent);
builder.addAction(R.drawable.ic_action_close, "Reject", rejectPendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
Just setting the vibration, makes it working fine.
builder.setVibrate(new long[0]);
The key is to call setContentIntent on the Notification Builder and passing it a PendingIntent. The Full code with comments explaining each step is included below. See the part named "THIS IS THE PERTINENT PART" (The Full code is included for completeness sake.)
// Use Notification Builder to start things off
// There are other ways of acquiring a Notification Builder; this is just an example
String channelId = "channelId";
String title = "title";
String body = "body";
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, channelId);
notificationBuilder
.setSmallIcon(R.drawable.ic_alarm)
.setContentTitle(title)
.setContentText(body);
//--------------- THIS IS THE PERTINENT PART ---------------
// Prepare Intent for creating PendingIntent
Intent intent = new Intent(context, ActivityToStart.class);
// Create Pending Intent
int requestCode= 1234; // requestCode has to be a unique ID for EACH PendingIntent
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(
requestCode,
PendingIntent.FLAG_UPDATE_CURRENT // use to prevent re-using current Activity Intent
);
notificationBuilder.setContentIntent(pendingIntent);
// Finally, create the Notification
Notification notification = notificationBuilder.build();
I create notification with action button. When clicking on action button, broadcast receiver is called. I am passing the notification ID in the intent
In the broadcast receiver I do the following
int notifId = intent.getIntExtra(Constants.NOTIF_ID, 0);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(notifId);
This is how I generate the notification
int notifId = Util.random.nextInt(9000);
Intent mIntent = new Intent(con, NotificationBroadcastReceiver.class);
mIntent.putExtra(Constants.NOTIF_CODE, codeReason);
mIntent.putExtra(Constants.NOTIF_ID, notifId);
PendingIntent mPendingIntent = PendingIntent.getBroadcast(con, 0, mIntent , 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(con)
.setSmallIcon(R.drawable.icon)
.setContentTitle("test")
.setPriority(Notification.PRIORITY_MAX)
.setAutoCancel(true)
.addAction(R.drawable.ic_launcher, "action", mPendingIntent);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(con, MainActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(
con, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) con.getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder.setDefaults(
Notification.DEFAULT_SOUND |
Notification.DEFAULT_VIBRATE |
Notification.DEFAULT_LIGHTS
);
// mId allows you to update the notification later on.
mNotificationManager.notify(notifId, mBuilder.build());
However the notification does not get hidden/dismissed although I know I am hitting the code ( using log statements).
Why is that?
I found the answer. I "think" that due to the 2 pending intents having the same con and req code, they are ending up modifying their intent values. I fixed it by using 2 different request codes to ensure unique Pending Intents . Therefore , I got the same notifId
Check your id if it's zero.
Check the notification is not binded with other Service by startForeground().
I am using GCM for push notifications. My constraint is that from the received bundle from GCM server I have to redirect user to specific location in my application. Every thing is working fine when I have only one notification. If there are two notification in the notification tray then the user is redirected to the activity based on the latest bundle. All other bundle are ignored. I have followed many SO post like this and this but it didnot solve my problem. Here are excerpts from my code:
private void sendNotification(String msg, String type, String id) {
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("notification_type", type);
intent.putExtra("notification_id", id);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent, 0);//I have tried all flags of pending intent like PendingIntent.FLAG_UPDATE_CURRENT and other 3
// flag_update_current
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("RWD Rugby").setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
Random rnd = new Random();
NOTIFICATION_ID = rnd.nextInt(90000);
Log.d("notification number", NOTIFICATION_ID + "");
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
On the receiving end I use :
Intent intent = getIntent();//this gives me extras from the intent that started the activity not pending intent
String fromNotification = intent.getStringExtra("notification_type");
I might have to compromise on the result that I might get. For me the best case scenario is that user is redirected to the correct page based on the property i set on creating the intent. Any help is much appreciated and if I am not specific enough please ask.
You can try the below code..
(try using FLAG_UPDATE_CURRENT)
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent myintent = new Intent(this, ReceiveActivity.class);
myintent.putExtra("message", msg);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
myintent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
// .setSmallIcon(R.drawable.ic_stat_gcm)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}