I'm trying to send notifications with FCM, it works as expected but i'm facing two different behaviours that I want to deal with because I'm using bigtextstyle and an action:
First case: App in foreground,
onReceiveMessage works as expected and notification is shown correctly
Second Case: App killed or in background,
the documentation says that messages are not handled in the onReceiveMessage event, and the layout of the notification is not the bigtextstyle one:
I tried to find a way for showing the desired layout, but nothing is working, they said that data payload is delivered in the extras of the intent of the launcher Activity!
So, what I want, is to show the same notification layout in the two cases, because some time I use the notification to trigger a call or to open a link in a browser not to open the app itself.
I sent my messages from Firebase Console as shown :
and here's the function that notify :
private void notifyMe(RemoteMessage rm){
String title = "";
String body = "";
String phone = "";
String link = "";
long time = System.currentTimeMillis();
Intent iCall;
PendingIntent pintCall = null;
Intent notificationIntent = null;
NotificationManager notificationManager;
notificationManager = (NotificationManager)FirebaseMessagingService.this.getSystemService(Context.NOTIFICATION_SERVICE);
if (rm.getNotification() != null) {
title = rm.getNotification().getTitle();
body = rm.getNotification().getBody();
}
if (rm.getData().size() > 0) {
phone = rm.getData().get("phone");
link = rm.getData().get("link");
if(link.contains("http://")){
notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
}else{
notificationIntent = new Intent(FirebaseMessagingService.this, MainActivity.class);
}
if(phone.equalsIgnoreCase("")){
iCall = new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phone, null));
pintCall = PendingIntent.getActivity(FirebaseMessagingService.this, 0, iCall, PendingIntent.FLAG_UPDATE_CURRENT);
}
}
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
PendingIntent pint = PendingIntent.getActivity(FirebaseMessagingService.this, 0, notificationIntent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(body)
.setWhen(time)
.setPriority(Notification.PRIORITY_MAX)
.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
.setContentIntent(pint);
NotificationCompat.BigTextStyle bigtext = new NotificationCompat.BigTextStyle();
bigtext.setBigContentTitle(title);
bigtext.bigText(body);
mBuilder.setStyle(bigtext);
if(!phone.equalsIgnoreCase("")){
mBuilder.addAction(android.R.drawable.ic_menu_call, "Call me!", pintCall);
}
notificationManager.notify(0, mBuilder.build());
}
Someone has a path on how to do this ?
Thanks.
The Firebase Notifications console sends so-called "notification messages". When your app is not in the foreground, these notifications messages are handled by the system itself and automatically displayed in the notification tray in the format you're seeing. When the user clicks this notification, the app is activated.
To ensure the message always gets delivered to your onReceiveMessage method, use "data messages".
For a great explanation, see the documentation on the different message types.
Related
Here is my code, When my app is running in foreground it opens targeted activity but when app is closed or in background, it doesn't opens targeted activity, please help me to solve this problem
I want that by clicking on notification will open targeted activity even app is running / closed.
public void onMessageReceived(RemoteMessage remoteMessage) {
session = new Session(this);
// if (session.getBscloggedin() || session.getAdploggedin()) {
// Log.d(TAG, "FROM:" + remoteMessage.getFrom());
//Check if the message contains data
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data: " + remoteMessage.getData());
}
//Check if the message contains notification
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Mesage body:" + remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getBody());
}
}
/**
* Dispay the notification
* #param body
*/
private void sendNotification(String body) {
//for CS101 announcement notification
if (body.contains("CS101")) {
Intent intent = new Intent(this, CS101noti.class);
intent.putExtra("body", body);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0/*Request code*/, intent, 0);
//Set sound of notifica tion
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notifiBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("mine")
.setContentText(body)
.setAutoCancel(true)
.setSound(notificationSound)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /*ID of notification*/, notifiBuilder.build());
}
Request code should be unique.
Generally people share with us example like giving request code 0(zero). I am the one of copy/paste developer sometimes. But recently I faced with error using this line.
PendingIntent.getActivity(myContext, 0, myIntent, 0);
Whenever app receives notification, intent activity receives old data because of requestCode. Here is the documentation about that method.
Returns an existing or new PendingIntent matching the given
parameters.
So second parameter(requestCode) should be unique to intent. If you use same request code for every notification, activity will be receive old data. So your requestCode should be unique.
There is also another solution.Use PendingIntent.FLAG_UPDATE_CURRENT as flag. The doc says that;
If the described PendingIntent already exists, then keep it but
replace its extra data with what is in this new Intent.
onMessageReceived callback is not called when you receive notification messages in background. If you receive this type of message in background, you can only handle that in a launcher activity.
The solution would be to either change the backend so that it sends only data messages and you handle them internally, or write some routing code inside your launcher activity.
use "click_action" attribute from your backend from which you are sending push notifications. in that attribute value, you have to pass the fully qualified class path of activity, which should be opened when you click on that notification.for more reference refer handling firebase push
Intent intent = new Intent(this, CS101noti.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
....
There are 3 cases in Push Notification.
Case 1 : App is already running and notification appears.
Case 2 : App is closed and notification appears but app is opened by clicking app icon
Case 3 : App is closed and app is opened from notification click
My question is how to detect whether app is opened from case 2 or case 3? If I able to detect than I can save some value in preference and using that value I can differentiate whether I have to open Main Activity or Notification Activity.
If you have better idea to decide which activity should be opened after splash (either Main Activity or Notification Activity) than Kindly tell me.
Notification notification = new Notification.Builder(context)
.setAutoCancel(true)
.setContentTitle("My Notification")
.setContentText("You have a received notification.")
.setSmallIcon(getNotificationIcon())
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
R.drawable.ic_launcher))
.build();
notification.defaults=Notification.DEFAULT_SOUND;
notification.number = notificationCount++;
Intent notificationIntent = new Intent(context, SplashActivity.class);
notificationIntent.putExtra("pushClicked", true);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
System.out.println("title="+title+"message="+message);
notification.setLatestEventInfo(context, title, message, contentIntent);
int SERVER_DATA_RECEIVED = 1;
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(SERVER_DATA_RECEIVED, notification);
In the Target(Splash) activity
boolean pushClicked = false;
if(getIntent()!=null){
pushClicked = getIntent().getStringExtra("pushClicked");
System.out.println("pushClicked="+pushClicked);
}
System.out.println(pushClicked );
Always getting false
Add an extra boolean value along with the intent created to open up application activity inside notification receiver.
For example :
#Override
public void onReceive(Context context, Intent intent) {
if (intent == null)
return;
Intent splashIntent = new Intent(context, TargetActivity.class);
splashIntent.putExtra("pushClicked", true);
context.startActivity(splashIntent);
}
Check for this boolean value inside the TargetActivity to distinguish between push click and app icon click.
I have a service that shows a notification PendingIntent each time it receives a new GCM message. The problem is that the GCM messages can be of different kinds. And if many notifications leave unread, I want not to show them separately but in groups like:
you have 3 unread messages of type A
you have 2 unread messages of type B
you have 4 unread messages of type C
As far as I understand, to get this effect I need to have an access to unread/unseen notifications. Each time when I new notification comes I can check, if there is another unread message of this type, and then decide, whether I create a new notification or update an old one.
My question is: is there a way to see, which notifications are unseen and get access to them?
For any case this is my method to create a message; if an argument notificationId is 0 a new notification should be created. Else - updated.
private int sendNotification(String msg, Integer notificationId) {
Log.d(TAG, "sending message with text: "+msg);
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Random random = new Random();
int notification_id = notificationId==0?random.nextInt(9999 - 1000) + 1000:notificationId;
RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.notification);
Intent intent = new Intent(this, MainActivity.class);
// Send data to NotificationView Class
intent.putExtra("text", msg);
PendingIntent pending= PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("escos")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(pending);
mBuilder.setContent(remoteViews);
remoteViews.setTextViewText(R.id.notiftext, msg);
remoteViews.setImageViewResource(R.id.notifim, R.drawable.ic_launcher);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mNotificationManager.notify(notification_id, notification);
return notification_id;
}
For different Notification strip (A, B, C etc.) in your status bar, use different NOTIFICATION_ID for building the Notification on basis of your defined type or collapse_key received from GCM.
For determining unread and read messages, use a local variable (counter) in Shared Preferences and increment it each time a specific type of Notification comes (on basis of defined type or collapse_key).
Then generate the Notification with that particular NOTIFICATION_ID as Notification with particular NOTIFICATION_ID can override each other. So You can override the previous Notification with Iterative Numbered text in New Notification.
As soon as user click on any Notification or particular Notification, clear the notification and reset the value of (counter) in Shared Preferences.
Edit1 : When you click on Notification with particular Pending Intent, then in that Activity use this code for removing all the Notifications generated from your app :
NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
try {
nMgr.cancelAll();
} catch (Exception e) {
e.printStackTrace();
}
Note : Do remember to add Try-Catch before you call cancelAll() as cancelAll() may not be supported by the device model and will generate
java.lang.SecurityException: Permission Denial
error.
Edit 2:
You can also use nMgr.cancel(NOTIFICATION_ID); to clear a specific notification, pass NOTIFICATION_ID to particular intent via extras and get the extras in that activity to cancel a particular notification.
And as you click on any notification it will be cleared from status bar unless you have not set .setAutoCancel(false) in your Notification Builder.
I have an app that kicks off a notification on a Wear device (the notification is created and shown on the Wear device).
What I want to do is have the "first" notification page display some high level info (app name, scroll to see more, etc...), and then have a list of pages containing content after that.
That works fine. The issue is that I want to attach an Action to each page (to kick off a PendingIntent). However, no matter what I try, I can't get the page to perform the Action.
I've tried:
setContentIntent
addAction
addAction and extend(new Notification.WearableExtender().setContentAction(0))
Anyone have any ideas?
I'm using Notification, not NotificationCompat, but I don't think that should make a difference.
UPDATE: I'm creating the notification on the watch. Here is the code I use:
private void createNotifications(ArrayList<Thing> things) {
DataApi data = Wearable.DataApi;
int notificationId = 0;
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder mainBuilder = new Notification.Builder(this)
.setSmallIcon(R.drawable.default_icon)
.setContentTitle("Things")
.setContentText("Swipe to see your things")
.setPriority(Notification.PRIORITY_DEFAULT);
List<Notification> pages = new ArrayList<>(things.size());
for(Thing thing : things) {
pages.add(createNotificationPageForThing(data, thing, notificationId).build());
notificationId++;
}
Notification n = new Notification.WearableExtender().addPages(pages).extend(mainBuilder).build();
nm.notify(notificationId, n);
}
private Notification.Builder createNotificationPageForThing(DataApi data, Thing thing, int notificationId) {
Asset bitmapAsset = getBitmapAsset(data, contact);
Intent thingIntent = new Intent(this, WearDetailActivity.class);
thingIntent.putExtra(WearDetailActivity.DETAIL_EXTRA, thing);
PendingIntent thingPendingIntent = PendingIntent.getActivity(this, notificationId, thingIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Action action = new Notification.Action(R.drawable.ic_action_social_person, "More details", thingPendingIntent);
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_action_social_person)
.setContentTitle(thing.getDisplayName())
.setPriority(Notification.PRIORITY_DEFAULT)
.addAction(action)
.extend(new Notification.WearableExtender().setContentAction(0));
if(bitmapAsset != null) {
try {
Bitmap b = BitmapFactory.decodeStream(
data.getFdForAsset(connection.getClient(), bitmapAsset).await().getInputStream());
builder.setLargeIcon(b);
} catch (Throwable ignore) {}
}
return builder;
}
#Eliezer, reading at the following line I did not understand what exactly is the behaviour you're experiencing ... explaining it in details might be helpful to debug your problem.
However, no matter what I try, I can't get the page to perform the Action.
Using .addAction() should work in your case. You'd want to use WearableExtender if you were creating the notification from the device, not the watch itself. Just to confirm something obvious - you're passing the .addAction a PendingIntent right?
Here's a code snippet from one of my applications, which accomplishes exactly what you're aiming for - I have just "Swipe for actions" text in the first page, and the next 2 pages perform different actions.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Intent openIntent = new Intent(this, CommonWearIntentService.class);
openIntent.setAction(CommonWearIntentService.ACTION_OPEN_ON_PHONE);
PendingIntent openPendingIntent = PendingIntent.getService(this, 0, openIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent addTransactionIntent = new Intent(this, CommonWearIntentService.class);
addTransactionIntent.setAction(CommonWearIntentService.ACTION_ADD_TRANSACTION);
PendingIntent addTransactionPendingIntent = PendingIntent.getService(this, 0, addTransactionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.swipe_for_actions))
.addAction(R.drawable.add_transaction, getString(R.string.add_transaction), addTransactionPendingIntent)
.addAction(R.drawable.common_full_open_on_phone, getString(R.string.common_open_on_phone), openPendingIntent)
.setVibrate(new long[]{500, 500, 500, 1000})
.setLights(Color.BLUE, 3000, 3000)
.setOngoing(true);
notificationManager.notify(ONGOING_NOTIFICATION_ID, builder.build());
Hope this helps!
Per the documentation, you must use NotificationCompat. After you switch to that setContentIntent() looks to be the right way to set the PendingIntent.
Example from the linked docs:
// Create builder for the main notification
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.new_message)
.setContentTitle("Page 1")
.setContentText("Short message")
.setContentIntent(viewPendingIntent);
// Create a big text style for the second page
BigTextStyle secondPageStyle = new NotificationCompat.BigTextStyle();
secondPageStyle.setBigContentTitle("Page 2")
.bigText("A lot of text...");
// Create second page notification
Notification secondPageNotification =
new NotificationCompat.Builder(this)
.setStyle(secondPageStyle)
.build();
// Add second page with wearable extender and extend the main notification
Notification twoPageNotification =
new WearableExtender()
.addPage(secondPageNotification)
.extend(notificationBuilder)
.build();
// Issue the notification
notificationManager =
NotificationManagerCompat.from(this);
notificationManager.notify(notificationId, twoPageNotification);
My app wants to bundle multiple push notifications in one icon in the status bar.
When clicking on the icon, the multiple notifications should be received by the app to show them in listview mode.
There are already some entries in stackoverflow which come close to what I want to obtain and it did give me a better insight in handling pending intents and notification flags but they didn´t solve completely my problem.
First step: Creating the notification:
Following some entries in stackoverflow, I made the following assumptions:
One notification ID (notifyID) to obtain only one icon in status bar
A unique requestId parameter in the pending intent to differentiate between the various notifications requests within the same notification ID
FLAG_ACTIVITY_NEW_TASK used in notification intent and FLAG_UPDATE_CURRENT used in pending intent
Notification notification;
int icon = R.drawable.ic_launcher;
int smallIcon = R.drawable.ic_launcher;
int notifyID = 1;
long when = System.currentTimeMillis();
int requestID = (int) System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, NewActivity.class);
notificationIntent.putExtra("new_message", message);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(context, requestID,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
int numMessages = 1;
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setNumber(numMessages)
.setSmallIcon(smallIcon)
.setAutoCancel(true)
.setContentTitle("You have " +numMessages+ " new messages.")
.setContentText(message)
.setWhen(when)
.setContentIntent(contentIntent)
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
notificationManager.notify(notifyID, mBuilder.build());
The way I see it, each time a notification is sent by GCM, my app generates a notification to send to the notification manager taking into account the previous assumptions.
First problem: if I want to notify the user with the number of pending notifcations left, how can I get track of the previous number of sent notifications? Do I have to store it in storage media?
Second step: Tapping the notification icon in status bar containing multiple notifications:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showNotifications();
}
public void onResume() {
showNotifications();
super.onResume();
}
public void showNotifications() {
if (getIntent().getExtras() != null) {
if (getIntent().getExtras().getString("new_message") != null) {
String newMessage = getIntent().getExtras().getString("new_message");
if (!newMessage.equals("")) {
handleMessage(newMessage);
getIntent().removeExtra("new_message);
}
}
}
}
public void onNewIntent(Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
if (intent.getExtras().getString("new_message") != null) {
String newMessage = intent.getExtras().getString("new_message");
if (!newMessage.equals("")) {
intent.removeExtra("new_message"); }
}
}
}
}
Second problem: I only receive the last notification sent. It seems to be that differentiating the pending intents with requestId didn´t do the trick. Also I thought that the different pending intents related to one notification icon would be handled by onNewIntent...
A possible solution that I was thinking about is to save incoming notifications from GCM in storage and get them on tapping the status bar icon, but for sure this cannot be the way Google meant it to be…
¿Any help?
for second step try this: PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);