This question already has answers here:
Open application after clicking on Notification
(12 answers)
Closed 3 years ago.
I have an android app which uses FCM for push notifications. There is a backend that sends push notifications
Every time user makes an image upload , a push notification is sent to whoever is subscribed to the topic.
On click of the notification in the notification tray I'm redirected to the first page of the app.
How can I make the push notifications work like how Instagram does.
On receiving a notification and clicking on it , I want to move to that particular screen and see the image that got uploaded.
If the user is in the app while the notification arrives. How to refresh the activity to display the content. Do I have to make a backend request to show the new data or can I display content from the notification itself?
Is there any example I can look at.
Any help will be appreciated. Thank you.
To start an activity that includes a back stack of activities, you need to create an instance of TaskStackBuilder and call addNextIntentWithParentStack(), passing it the Intent for the activity you want to start.
As long as you've defined the parent activity for each activity as described above, you can call getPendingIntent() to receive a PendingIntent that includes the entire back stack.
// Create an Intent for the activity you want to start
Intent resultIntent = new Intent(this, ResultActivity.class);
// Create the TaskStackBuilder and add the intent, which inflates the back
stack
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(resultIntent);
// Get the PendingIntent containing the entire back stack
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Related
I have set up a notification that opens a website when you tap it. Here's the part of the code that does it.
Intent resultIntent = new Intent(Intent.ACTION_VIEW);
resultIntent.setData(Uri.parse(m.msg.url));
PendingIntent pending = PendingIntent.getActivity(context, 0,
resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notif.setContentIntent(pending);
The notifiaction corresponds to a notification in a website, wich also corresponds to a message in that website. So when you tap the (Android) notification, you go to that website. But I also want to remove the (website) notification by sending a GET request. I already have set a method that does that:
m.delete();
However, I can't find a way to execute both actions at once. The intent should open m.msg.url and execute m.delete(). I have searched for information on Intents and Services but I'm new to Android programming and I don't quite understand how it works. I'd really aprecciate any help or guidance.
Thanks for reading.
Create an Activity or Service that performs the GET then immediately starts the activity you really want to start. Use that as the PendingIntent instead.
I'm configuring my notifications to open a new instance of ActivityDetail on click:
Intent resultIntent = new Intent(getApplicationContext(), ActivityDetail.class);
resultIntent.putExtra("object_id", objectId);
...
TaskStackBuilder stackBuilder =TaskStackBuilder.create(getApplicationContext());
stackBuilder.addNextIntentWithParentStack(resultIntent);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT);
I build each notifications with a different id, as clicking will populate the ActivityDetail with results unique to the notification.
When I receive two notifications, the first one will open ActivityDetail correctly. Clicking the second notification, however, does nothing (except dismiss it), whether I remain on the ActivityDetail screen or not. Specifying the activity launch mode as "singleTop" makes no difference.
I don't seem to have any problems if there is only one notification displayed at a time, but if there are two, the second one always fails to open a new instance of the ActivityDetail on click. Any help would be appreciated.
I think this is happening because you are sending static request code '0' in 'getPendingIntent' method while obtain pending intent. send a separate value for each notification instead of '0'. it will work.
You need to do like this:
PendingIntent resultPendingIntent =stackBuilder.getPendingIntent(SEPARATE_INT_VALUE, PendingIntent.FLAG_CANCEL_CURRENT);
// Now change value of 'SEPARATE_INT_VALUE'
I've a problem with my application. I use a class to manage push notification from Azure, extending NotificationsHandler.All works,the method onReceive "catch" the incoming notification,using bundle i can read each field of the json from azure server.If i click on the notification i can start the activity as follows:
fragment_richiesta_tabsV2 dettaglioTabs= new fragment_richiesta_tabsV2();
Intent myIntent = new Intent(contesto, dettaglioTabs.getClass());
contentIntent = PendingIntent.getActivity(contesto, 0,myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
the problem is when the app is closed from the recent apps.The notification arrives,but if i click on the app crash...how can i solve?
thanks
I have a method which receives text from a push notification, via the Parse API, and packages it into a notification object. Pretty standard stuff. My problem is that I'm trying to use a BigTextStyle to display my notification in the list, but it refuses to do so, and only shows one line of text and the two-finger gesture does not cause it to expand.
However, if I tap the notification, which opens the app, then return to the notification list, it is displayed in the BigTextStyle and is responsive to gestures. So, my guess is that somehow tapping on the notification is activating it and allowing the BigTextStyle code to kick in.
I like that tapping on the notification opens the app, but I don't want to force my users to open the app then close it again to see the full text of my messages. So is there a way I could either make the notification display in the BigTextStyle format from the start, or to make it so that the first click "activates" the notification, allowing the full message text to be seen, and then a second click opens the app? Any help would be appreciated.
Here is my code from the Notification method:
public void receiveNotification() {
NotificationCompat.BigTextStyle bts = new NotificationCompat.BigTextStyle();
bts.bigText(SplashActivity.globalDataString);
bts.setSummaryText("Tap to open app, swipe to dismiss message");
NotificationCompat.Builder m = new NotificationCompat.Builder(this);
m.setContentTitle("New Push Notification")
.setContentText(SplashActivity.globalDataString)
.setSmallIcon(R.drawable.app_icon)
.setStyle(bts)
.build();
Intent openApp = new Intent(this, MenuActivity.class);
// This ensures that navigating backward from the Activity leads out of
// the application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MenuActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(openApp);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
m.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(pushMessageID, m.build());
pushMessageID++;
//reset notification
flag1 = false;
}
EDIT: I think my problem is with where I'm calling my receiveNotification() method from. Right now I have it in the onCreate() method of my app's starting activity, which doesn't make much sense looking back. Should I put it in my broadcastReceiver class, or would there be a better place to put it?
Yes, the creation and display of the notification is usually done either in the broadcast receiver, or in an intent service started by the broadcast receiver. Only when the user taps the notification, the relevant activity is launched. You can see Google's client code sample here.
I have a service that shows a notification that I wish that will be able to go to a specific activity of my app each time the user presses on it. Usually it would be the last one that the user has shown, but not always.
If the activity was started before, it should return to it, and if not, it should open it inside of the app's task, adding it to the activities tasks.
In addition, on some cases according to the service's logic, I wish to change the notification's intent so that it will target a different activity.
How do i do that? Is it possible without creating a new notification and dismissing the previous one? Is it also possible without creating a new task or an instance of an activity?
No it wouldn't be possible to change the Activity once you have sent the notification.
You can start an Activity on your task stack that is not a problem, check out the notification service in the tutorial here:
http://blog.blundell-apps.com/notification-for-a-user-chosen-time/
You have to set a pending intent on the notification:
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, SecondActivity.class), 0);
// Set the info for the view that shows in the notification panel.
notification.setLatestEventInfo(this, title, text, contentIntent);
You can see the pending intent takes a normal intent "new Intent(this, SecondActivity.class" so if you want specific behaviour (like bringing to the top instead of starting a new activity. Add the flags like you would normally to this intent. i.e. FLAG_ACTIVITY_REORDER_TO_FRONT (something like that)
Since platform version 11, you can build a notification using Notification.Builder. The v4 support library has an equivalent class NotificationCompat.Builder.
You can't change the Activity once you've sent the notification, but you can update the notification with a new Intent. When you create the PendingIntent, use the flag FLAG_CANCEL_CURRENT. When you send the new notification, use the ID of the existing notification when you call NotificationManager.notify().
Also, you should be careful how you start your app. The Status Bar Notifications guide tells you how to set up the back stack.