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.
Related
We are working on notification trampolines on Android 12.
Originally our app launches an activity by a broadcast receiver.
I found out that using PendingIntent.getActivity instead of PendingIntent.getBroadcast would solve the problem.
Regarding this, I have a following concern.
When the broadcast receiver is used, i.e. when PendingIntent.getBroadcast is used, I programmed so that the broadcast receiver determines whether to launch the app.
However, I no longer use the broadcast receiver due to notification trampolines. Therefore, PendingIntent.getActivity launches the app without choice.
I would like to know if there is any way to determine whether to launch the app depending of the state of app without using the broadcast receiver.
For example;
when App is in state A:Launch the app with a push notification tap
when App is in state B:NOT launch the app with a push notification tap
sort of workaround would be to launch some dedicated Activity, which may be set as fully transparent without any enter/exit animation, noHistory flag etc. and in there you may run your checking logic - starting "real" Activity or just finish() if there is no need
I'm using a transparent activity to handle this issue. all the notification related works are handled in the transparent activity.
Intent intent = new Intent(mContext, NotificationActivity.class);
intent.putExtra("notification", parseInt(this.mActionDetail.getNotifyId()));
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
notificationManager.notify(parseInt(this.mActionDetail.getNotifyId()), builder.build());
create a transparent activity NotificationActivity.class then you can identify the application state then you can decide the action
I wante to create some notifications to user.
Ex : when a new comment, when someone ask question,...
I know how to create notification, but i don't know how can i do for send a notification just one time.
Example : notification is send to user because he has a new comment. He click on the notification for start application and watch the comment. But now, how can i avoid that it receives twice the same notification ?
It is recommended to store it in a database, in "notification" table or it exist a system for manage this ?
I'm sorry for the very bad English.
Have you looked at Google Cloud Messaging?
https://developers.google.com/cloud-messaging/android/start
If you've already implemented this, and you're receiving the same notification twice, it might have something to do with how you are building the Notification. If the link above doesn't help, can you post more information, and possibly the code?
I solved the problem by checking if the notification is already displayed.
private boolean isNotificationVisible() {
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent test = PendingIntent.getActivity(context, MY_ID, notificationIntent, PendingIntent.FLAG_NO_CREATE);
return test != null;
I am building an app that needs to perform an action at a specific date and time and put a notification an hour before this task is performed.
I have searched and found that the Alarm Manager class can be used for this purpose but I am not sure how to use it. Also if in a future point to cancel or edit it once the application is closed and reopened.
I have created a SQlite database which stores the information along with the date and time of the event to happen.
Thanks
Google provides a "Scheduler" code sample which does what you want: display a notification at a given time. This sample also shows how to cancel an alarm.
Edit : I've just watched at the sample code again and you are right, the sample doesn't explain how to get the PendingIntent after the app is closed and reopened. The following code creates the PendingIntent if it doesn't exist, and if it already exists, it returns the existing PendingIntent, thanks to FLAG_UPDATE_CURRENT. So you don't have to care if the alarm has already been started or not: just get the Pending intent, and cancel it.
Intent intent = new Intent(context, MyAlarmReceiver.class);
pendingAlarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
....
pendingAlarmIntent.cancel();
i'm a beginner in android , so excuse me for a perhaps stupid question. I'm developing an App with different Features, wich are chosen in the Menu. By google cloud Messaging the App is also receiving push notifications, which are Stored in an MySQL database. These notifications can be shown in a second listactivity. Now my Problem: when this activity is open and a notification is coming in, it is Stored in database, but the aktive listactivity is not updating, Cause it doesn't know. Howe can i Force my listactivity to Refresh from mainactivity when mainactivity is receiving a notificatipn ? Thanks from Germany Fritz
You can post o a notification (for example from a Service) and pass an intent to the corresponding pendingintent.
Intent intent = new Intent(this, MyListActivity.class);
intent.setAction(Intent.ACTION_VIEW);
PendingIntent pi = PendingIntent.getActivity(this, my_code, intent, PendingIntent.FLAG_UPDATE_CURRENT);
...
notificationManager.notify(my_notification);
And in your MyListAcitity you can override the method onNewIntent and reload your data.
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.