Force second activity to refresh (from mainactivity) - android

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.

Related

How to make a notification tap in Android perform multiple actions?

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.

Unable to retrieve new intent extras

I'd like to ask you for help as after trying to figure this issue out for a couple of hours still can't get it works.
I have a notification manager which process incoming GCM messages and creates notifications, however an intent, that is passed to pending intent, always got old extras (intent recycle) within activity.
intent.putExtra("user_id", id);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
builder.setContentIntent(PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
Lets say that I will receive two notifications from two different users and intent starts the same activity which displays that user ID. After click on first notification, activity is launched as usually and intent's extras contains user ID of first user. However, if I will remain within this activity, and click on another push notification, an activity is recreated (onDestroy is called) but, intent's extras contains user id of first user, not the second one.
Here is my question. How can I retrieve new intent extras? I've already tried to implement onNewIntent callback method, but it never get called, also tried to change flags but unsuccessfully and what's kinda weird to me is, that even after onDestroy callback is called, intent extras in next instance of that activity have old data...
Thanks in advance
You need to pass unique Id in place of just 0 when fetching Activity from PendingIntent:
int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);
builder.setContentIntent(PendingIntent.getActivity(context, iUniqueId, intent, PendingIntent.FLAG_UPDATE_CURRENT));
I think you fire your notifications either with the same id or with a inappropriate intent flag. As it is mentioned here, when creating the pending intent you can set its flag. If you don't like the previous pending intent to be updated or overridden, you should set its flag to FLAG_ONE_SHOT. It indicates that although you have more than one pending intents sticking around in the system, each can be executed only once!
Conclusion: Your code should be sth like this:
PendingIntent pIntent = PendingIntent.getActivity(context, id,intent,PendingIntent.FLAG_ONE_SHOT);
In this code the "id" is unique per pending intent and "intent" is the actual intent for the target activity.
Cheers

App crash (if closed) after click on notification

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

Change notification intent in Android

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.

Service notification open main activity

Ok,
I am working on an Android app that uses a service to collect & process GPS data. The service also creates a notification in the status bar. What I cannot figure out is how to have the notification open the main class when a user clicks on it. From what I've found online it shouldn't be that hard to do, but I cannot get it to work. Does anyone have some suggestions on where to look?
Intent notificationIntent = new Intent(this, MyClass.class);
This line of code is from http://developer.android.com/guide/topics/ui/notifiers/notifications.html.
Simply replace MyClass with your activity class - it will be launched when you click the notification.

Categories

Resources