I'm experiencing this problem: If I click on the notification and my app is closed, HomeActivity is opened instead of the requested one.
I'm raising the notification like this:
Intent intent = new Intent(context, ArticleActivity_.class);
intent.putExtra(ArticleActivity.KEY_ARTICLE_ID, articleId);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0 , intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(notification.getTitle())
.setContentText(notification.getBody())
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
Notification n = notificationBuilder.build();
n.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 , n);
And everything works fine if the application is opened. But if I close it, after clicking on the notification, HomeActivity will be opened instead of the one I configured.
You are having this problem because that is how FCM works. There are 2 types of push notifications. The first type is "notification" and the second type is "data". Both can be visualize as a JSON payload:
"notification": {
//key values
}
"data": {
//your custom key values
}
There is a third type which is a combination of both.
The "notification" type will create a default visual notification with default behavior when the app is not in the foreground. And it will execute whatever your write inside onMessageReceived when the app is open. The second type "data" will always do what you code inside onMessageReceived.
From the Firebase web console, you can send "notification" type or the combined type. For sending only "data" you need to send it from Firebase Functions or from a Server (that complies with the FCM requisites).
There is a trick you can do to get your goal. Combined payload, that can be send from the Firebase web console, contains "notification" and "data". The "data" key-values are available after the user click the notification. That information is available as extra data from the intent opening the default launcher Activity, so there you can change your Activity.
In your launcher Activity, add the following:
String notification = getIntent().getStringExtra("your-key");
if (notification != null) {
startActivity(new Intent(this, YourActivity.class));
}
This way you can validate that the Activity is being opened because the default notification was clicked, and is always a String because payload in push FCM must be by rule always String. In other cases, if the user simply opened the app, then the String will be null, and you don't redirect the user.
Related
I am making an app for my university, now in which I want to send messages/information about events. How I can send info to them. I tried firebase but, it is working till push notification only. I want if someone clicks on the notification it should open a new activity with whole message/info any code reference will be very helpful.
use the payload to put information into your notification. this could be an event id. The text of your notification is is also set by your server.
Thanks to the payload, your app client knows what event ID was received with the notification (when notification was opened). Just ask your database about information from the received event id from the client and you have all you need.
public void getnotification() {
NotificationManager notificationmgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(this, your_acitivity.class);
PendingIntent pintent = PendingIntent.getActivities(this, (int) System.currentTimeMillis(), new Intent[]{intent}, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notif = new Notification.Builder(this)
.setSmallIcon(R.drawable.shadow_fiend)
.setContentTitle(notif_title)//Title of the notification
.setContentText(notif_detail)//Description of the notification
.setContentIntent(pintent)
.setAutoCancel(true)
.build();
notificationmgr.notify(0, notif);
}
When the above function is called a notification is generated and by clicking the notification the user is redirected to a specific activity. In the intent replace the your_acitivity.class to whatever activity you want to be redirected when notification is clicked.
Feel free to comment if I misunderstood your requirements.
Could you help me to solve the problem?
The task is develop push notification that will open particular Activity by users tap.
I've added intent-filter and tried to send the push message with the click_action field, and everything works like a charm.
The problem is with the previous version of the application where Intent-filter was not added yet. If user tap on the push message (in the case of previous app version), nothing happens.
What is better to do in this case? How to handle such situations?
Edit:
So the question is, how can we have default behavior (opening the app on the last screen) in the case of specific screen mentioned in click_action field and lacking of necessary intent-filter? This relevant for situation when the user has previous version of the app without necessary intent-filter in the manifest file.
To perform some action on push-notification click we need to write code for that to perform such action. In your case you want to perform some action on your old live application, In this can you can't do anything.
To perform some action you can call this method on push notification received
private void sendNotification(String title, String messageBody) {
Intent intent = new Intent(this, YourActivityName.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(messageBody)
.setVibrate(new long[]{01})
.setAutoCancel(true)
.setContentIntent(pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
notificationBuilder.setSmallIcon(R.mipmap.notification);
else
notificationBuilder.setSmallIcon(R.mipmap.notification);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notificationBuilder.build());
}
Help me out friends I have been facing a problem from a week
I have made an android app in which I got 3 activities named
MainActivity
NotificationListActivity
NotificationActivity
other than that I have 2 services in my app ( as because I am using FCM )
JLZFirebaseMsgService
JLZInstanceIdService
in JLZFirebaseMsgService.java I have written the code
private void sendNotification(String messageBody) {
Intent intent = new Intent(this,NotificationActivity.class);
// intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Jee Lo Zindagi 2.0")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
what I want is to whenever FCM server sends the notification to the devices the app should open NotificationActivity as the I wrote
but whenever I tap on the notification it opens the MainActivity I don't know why.
when the app is open the notification comes and by tapping on it we go to the NotificationActivity that's what I wanted but not the same scenario when the app is closed.
but sometimes it happens that the app shows the correct activity by tapping on the notification I still don't know why.
You should use "data" insted of "notification" on your server code
When you use "notification" on your server code
while you app is not running the JLZFirebaseMsgService.java does not start . instead the you will recevie a notification with your launcher icon
But if you use "data" on your server code your messaging service will start
It seems that you are sending a notification messages, these are messages that go to onMessageReceived when your app is in the foreground in your case that will result in you manually generating a notification, when your app is in the background it will result in an auto-generated notification that your code does not define.
If you are using the FCM REST API then you can use the click_action field which would allow you to define which activity would be launched when the user taps on the notification. If you are using the Firebase console there currently you cannot specify a click_action from there.
I'm developing an app and use push notification. My app contains a list of IDs. Clicking on particular id, I am able to see the details related to the id.
The push notification contains an id and so I want that if I click on the push notification then it will open the details page of the id for display details related to the id from the notification.
For e.g.
In my app, I have number of Ids (for example 123,456,657...) and if I clicking on 123, it will show the details related to the id like name, phone no, emails etc.. and my push notification contain 123, I want that if I clicked on push notification, it will open the details page of 123 id.
Using PushNotification you are passing some id for your items in application's local database or WebService calling id or whatever..
Create a pending intent for that DetailActivity and pass this id in bundle for this.. and write code to getId from bundle here and based on that get data from database or WebService..
if any query feel free to ask..
You must put the id as an extra for intent associated to notification:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true);
// Create intent and set extra
Intent targetIntent = new Intent(this, <YourDetailsActivity>.class);
targetIntent.putExtra("id", id);
// Associate intent to notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Create notification...
Notification notification = builder.build();
// ...and show it
NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(<YOUR_NOTIFICATION_ID>, notification);
I don't have any experience with notifications and after a long time now, I'm feeling really stupid, that I can't make it:
I'm getting push notifications from my server. On click of one, I'm getting the inormations with
getIntent().getBundleExtra("extra");
in the onResume method.
But if I'm getting a lot of notifications and don't open my application between this, then I'm just getting the last, if I click on the notification. Where are the notifications before? The best would be, if I'm getting a list of bundles or something like this. How can I do that?
Thanks for help, I'm really depressed :)
EDIT:
For better understanding: After receive a GCM I'm make a notification like this:
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, title, System.currentTimeMillis());
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
if (extras != null && launchIntent != null) {
launchIntent.putExtra("extra", extras);
}
PendingIntent intent = PendingIntent.getActivity(context, -1, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, title, msg, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(0, notification);
Example: I get a GCM and set a notification. If I don't open my application, but get the next GCM, then my first notification will replaced by the new one. This could be many times. After a few notifications i click on it. If the app starts I will get all the notifications, which has been sent. At the moment I just get the last one. How can I get all?
mNotificationManager.notify(0/* 0 is your notification id */, notification);
creates a notification with id 0. Now when you receive another notification and you notify it with the same notificaionId (i.e 0). Hence the latter pending Intent replaces the previous pendingIntent.
Check the explanation for notify() in the docs =>
http://developer.android.com/reference/android/app/NotificationManager.html
The best option would be to save your data locally in some DB, preferences or the traditional way
File and whenever you receive a push notification from GCM save it locally and then notify.