Push Notification Onclick it should open the application - android

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);

Related

Can't start Application to particular activity on Notification click from FCM

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.

I want to send information through notification

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.

Some notifications are clickable some are not

I am getting multiple Local notifications of range 1-10..
I am getting getting notifications with its content and title.. but when i click the notification only first notification open and when i click rest the notification disappear and it show activity but the content remain the same that one of first notification.. Here is my code
on clicking notification i applied this code
Intent intent1 = new Intent(context, Message_activity.class);
intent1.putExtra("randomStr", randomStr);
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pndng = PendingIntent.getActivity(context, id, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
and getting like this in message activity
String message = getIntent().getStringExtra("randomStr");
Log.e("randomStrrandomStr", getIntent().getStringExtra("randomStr"));
fullmsg.setText(message);
Please suggest something Thanks in advance
I guest you used same id for each time you notify a notification by call notify method of NotificationManager. So try to use different id for each time you call notify.

How maintain GCM push message in Android, its replaced by new one

My Android application receives push notification with some text messages.If I tap a push it redirects me to desired activity with latest push message (intent message) but I want to show my desired activity with corresponding push messages.
For example If I receives 10 push notifications and I tap 3rd notification, my code redirects me to the specified activity with 10th push notification's message, but I want to show 3rd intent push notification's message.
I know PendingIntent.FLAG_UPDATE_CURRENT replace the intent message, how can I redirect with corresponding message instead last message?
I have tried the following:
Intent intent = new Intent(this, TestActivity2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra("uid", uid);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
getApplicationContext());
Notification notification = mBuilder
.setSmallIcon(R.drawable.ic_launcher)
.setTicker(textMsg)
.setWhen(0)
.setAutoCancel(true)
.setContentTitle(textMsg)
.setStyle(
new NotificationCompat.BigTextStyle().bigText(textMsg))
.setContentIntent(resultPendingIntent).setContentText(textMsg)
.build();
NotificationManager notificationManager = (NotificationManager) getApplicationContext()
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) System.currentTimeMillis(),
notification);
Use FLAG_ONE_SHOT instead and change the second parameter in getActivity which is set to 0. This answer will make it clear.
You should Change the pendingIntend's content for Different Messages.
Here is the snippet from PendingIntent document.
"A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it. If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid, and can thus call cancel() to remove it."
In simple words...try passing different id(Something like currentEpochTime) for each pendingIntent.

How to disable multiple push Notification icon in Android, Am using Parse.com

com push notification for Android app, All is working well but lets say if i send 5 push notification a day, i dont want user to see 5 notification icons at once on his phone. Its best if he sees only one.So is their any way where old notification is deleted and only latest is shown.
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
The method that displays the notification has an int parameter which represents the notification identifier. If you use a constant identifier, each new notification will replace the previous one.
you have to set the notification ID to be the same value. So then they get replaced as each one arrives.
int notificationId = 1 ;
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setAutoCancel(true);
builder.setSmallIcon(R.drawable.gcm_logo);
builder.setContentTitle("Test Title");
builder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, builder.build());
So you can maintain notification Id for different type of notifications. If you keep same notification ID for each notification then it would be updated in the same .
If you have any extras in the pending intent and you want to update those extras with the latest ones then generate pending intent with :
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(),
PendingIntent.FLAG_ONE_SHOT);

Categories

Resources