Calling onNewIntent() from a notification - android

MainActivity hosts all of my fragments.
I need to display one particular fragment when the user clicks on a Notification that was received from Google Cloud Messaging.
I was under the impression that I could handle this from onNewIntent(), but this method doesn't seem to fire after I press the notification.
Here's my send notification method in my GcmIntentService class:
private void sendNotification(String message, String eventId) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.putExtra(Constants.eventIdBundleKey, eventId);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_test)
.setContentTitle(getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message))
.setContentText(message);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
and here in MainActivity, where I'm trying to receive and process:
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if(intent.hasExtra(Constants.eventIdBundleKey)) {
Fragment fragment = GiftsPriceFragment.newInstance(Common.retrieveAppUser(this), null, null);
mFm.beginTransaction().replace(R.id.content_frame, fragment)
.addToBackStack(GiftsPriceFragment.FRAGMENT_TAG).commit();
}
}
Any idea why onNewIntent() doesn't get called?

Try setting launchMode of your activity as "singleTop" in manifest.
From Official documentation.
This is called for activities that set launchMode to "singleTop" in their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.
An activity will always be paused before receiving a new intent, so you can count on onResume() being called after this method.
Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.

Related

How do I update Activity with intents passed via Notification

I have created a custom notification, but when i pause the activity with the home button and a notification arrives, i press the notification and it creates a new activity and doesnt resume the previews activity and when i press the back button it goes to the previews one which is the same window. I have tried singleTop, singleTask, singleIntent, it works but it doesnt update the activity when a message enters, it is like the previews activity was in pause. how can i fix it?
if there is no solution i though in resuming the activity or destroy the paused activity or maybe restart the activity, is there a way to do this?
public void CustomNotification(String strtext) {
// Using RemoteViews to bind custom layouts into Notification
RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.customnotification);
// Set Notification Title
String strtitle = getString(R.string.customnotificationtitle);
// Open NotificationView Class on Notification Click
Intent intent = new Intent(this, NotificationView.class);
// Send data to NotificationView Class
intent.putExtra("title", strtitle);
intent.putExtra("text", strtext);
intent.putExtra("String T", "");
//intent.putExtra("Integer C", 0);
// Open NotificationView.java Activity
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
// Set Icon
.setSmallIcon(R.drawable.ic_launcher)
// Set Ticker Message
.setTicker(getString(R.string.customnotificationticker))
// Dismiss Notification
.setAutoCancel(true)
// Set PendingIntent into Notification
.setContentIntent(pIntent)
// Set RemoteViews into Notification
.setContent(remoteViews);
// Locate and set the Image into customnotificationtext.xml ImageViews
remoteViews.setImageViewResource(R.id.imagenotileft,R.drawable.ic_launcher);
remoteViews.setImageViewResource(R.id.imagenotiright,R.mipmap.ic_action_chat);
// Locate and set the Text into customnotificationtext.xml TextViews
remoteViews.setTextViewText(R.id.title,getString(R.string.customnotificationtitle));
remoteViews.setTextViewText(R.id.text, strtext);
// Create Notification Manager
NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Build Notification with Notification Manager
notificationmanager.notify(0, builder.build());
}
LOGS:
04-29 01:29:10.453 29001-29001/com.example.example E/STATE﹕NEW ACTIVITY
04-29 01:29:15.376 29501-29501/com.example.example D/Activity﹕ Activity.onPause(), editTextTapSensorList size: 0
04-29 01:30:06.981 29501-29501/com.example.example E/STATE﹕ RESUMING
WHEN I PRESS THE NOTIFICATION:
04-29 01:33:09.654 33449-33530/com.example.example E/STATE﹕NEW ACTIVITY
I will appreciate any answer, Thanks!
EDITED
ANSWER:
the answer was to add:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
because when i was in the same window and i pressed the home button, the activity was in paused, then if some notification arrived it brought me to that window but didnt updated the messages so I needed to create a new intent and erase the previous one which was in pause. so that code did the Job.
To update your activity, you should override onNewIntent():
protected void onNewIntent (Intent intent) {
super.onNewIntent(intent);
//reload your data here
}
Android Docs say:
void onNewIntent (Intent intent)
This is called for activities that set launchMode to "singleTop" in
their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag
when calling startActivity(Intent). In either case, when the activity
is re-launched while at the top of the activity stack instead of a new
instance of the activity being started, onNewIntent() will be called
on the existing instance with the Intent that was used to re-launch
it.
An activity will always be paused before receiving a new intent, so
you can count on onResume() being called after this method.
Note that getIntent() still returns the original Intent. You can use
setIntent(Intent) to update it to this new Intent.
Note: as the docs say, you need to set android:launchMode="singleTop" in your Activity tag (in AndroidManifest).
try this in your service code to use notification :
private static final int NOTIFY_ME_ID = 1222;
NotificationManager mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
private void Notifi(String message){
int requestID = (int) System.currentTimeMillis();
Intent intent = new Intent(Intent.ACTION_MAIN);
//CLEAR PREVIOUS ACTIVITIES AND CREATE A NEW ONE
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setComponent(new ComponentName("packagenameofyourapplication",
"packagenameofyourapplication.NotificationView"));
PendingIntent pintent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notif = new Notification.Builder(this)
.setContentTitle(getString(R.string.notification_titel)).setContentText(message)
.setSmallIcon(R.drawable.ic_launcher).setAutoCancel(true)
.setContentIntent(pintent).build();
notif.flags = Notification.FLAG_NO_CLEAR;
mNotificationManager.notify(NOTIFY_ME_ID, notif);
}

Catch two notifications in android

I am creating notifications with following code:
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setContentTitle(messageBody)
.setAutoCancel(true)
.setSmallIcon(R.drawable.top_icon)
.setContentText(senderName)
.setTicker(senderName+" ")
.setSound(soundUri);
Intent resultIntent = new Intent(this, LoginActivity.class);
resultIntent.putExtra("gcm_username",senderName);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_ONE_SHOT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NotificationID.getID(senderName), mBuilder.build());
When user clicks the notification I am catching it with following code in loginActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
gcmUserId= getIntent().getStringExtra("gcm_username");
startAction(gcmUserId);
....
My problem starts here.
Scenario:
1)When app is closed user receives notifications from 2 different users
2)User clicks first notification and app starts then startAction method calls.
3)Then user clicks the second notification
But when user clicks the second notification app has already started so startAction won't be able to call again because it is in the onCreate method.How can catch second notification ?
You can handle it inside onNewIntent(). You would need to override that method inside your activity. The docs say:
This is called for activities that set launchMode to "singleTop" in their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.

Which function will be called on starting an activity with a click on notification with a pending intent if the activity is already running

I want to open an activity when user clicking on a notification. I can do it with a pending intent which works fine even if the activity already running. But I want to do some thing on the click operation. now I am doing that in onCreate method. it only works when the activity not running. I tried with onNewIntent method which is not reaching on above condition. What I can do now ?
NOTIFICATION_ID => A number used to identify the notification to do further operations.
mContext => Application context.
ResultActivity => Activity to be opened.
//creating intent
Intent resultIntent = new Intent(mContext, ResultActivity.class);
resultIntent.putExtra("update_request", true);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
//creating pending intent
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mContext,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
//Build the notification using Notification.Builder
Notification.Builder builder = new Notification.Builder(mContext)
.setSmallIcon(R.drawable.icon)
.setAutoCancel(true)
.setContentTitle("Title")
.setContentIntent(resultPendingIntent) //adding pending intent
.setContentText("content");
NotificationManager mNotificationManager =
(NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
//Get current notification
Notification mNotification = builder.getNotification();
//Show the notification
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
Now you can do your operations on protected void onNewIntent(Intent intent) function of the Resulting activity.
And you also can set the new intent as intent of the activity as setIntent(intent) from the newIntent function.
Use PendingIntent.FLAG_UPDATE_CURRENT flag in pending intent to update the current activity. And then implement your login in onResume()

Starting activity from a service and updating its content, is there a way?

I am using a service to receive some GCM messages from the could. I put the message into an extra and passing it to the class I want to start, there I display the message inside a dialog.
But the next time I get a message it should update the content of the intent opened earlier, the problem is that I get an exception and my app crashes, eclipse is forcing me to use "FLAG_ACTIVITY_NEW_TASK" to solve the crash, but this way my "PendingIntent.FLAG_UPDATE_CURRENT" will be ignored.
How could I update the previously opened intent? Creating a third activity and passing the data back and fort seems to me a bad solution.
Service class:
...
private void sendNotification(String msg, Bundle extras)
{
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, DriverDetails.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
// If this is a notification type message include the data from the message
// with the intent
if (extras != null)
{
intent.putExtras(extras);
intent.putExtra("message", msg);
intent.setAction("com.myGcm.NOTIFICATION");
startActivity(intent);
}
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_cloud)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg)
.setTicker(msg)
.setAutoCancel(true)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
...
You're solution should be LocalBroadcastManager. It is pretty simple: just register a listener in the onResumeof your Activity and fire an Intent with the Update-Information from anywhere else. I attached two links wich should guide you ;)
how to use LocalBroadcastManager?
http://www.intertech.com/Blog/using-localbroadcastmanager-in-service-to-activity-communications/

Determine if Activity is called by a Notification

I am using an Activitiy with various Tabs on it. From a different part of the application, Notifications are created to tell the user that something has changed. I now managed to Call the Activity, when the user clicks on the Notification. But how can i determine wheter a Activity is created the "normal" way during runtime or by clicking on the notification?
(Depending on the notification clicked, i want to forward to another tab instead of showing the main Tab.)
Intent intent = new Intent(ctx, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, intent, 0);
// TODO: Replace with .Build() for api >= 16
Notification noti = new Notification.Builder(ctx)
.setContentTitle("Notification"
.setContentText(this.getName())
.setSmallIcon(R.drawable.icon)
.setContentIntent(pendingIntent)
.setDefaults(
Notification.DEFAULT_SOUND
| Notification.DEFAULT_LIGHTS)
.setAutoCancel(true)
.getNotification();
NotificationManager notificationManager = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);
// Hide the notification after its selected
notificationManager.notify(this.getId(), noti);
This successfully calls my MainActivity. But is there some Method that is called when the Activity is triggered by the pendingIntent?
Thought about to define something like this in the Main Activity:
onTriggeredByNotification(Notification noti){
//determinte tab, depending on Notification.
}
Pass a boolean value from notification and check for the same in the onCreate method of the activity.
Intent intent = new Intent(ctx, MainActivity.class);
intent.putExtra("fromNotification", true);
...
if (getIntent().getExtras() != null) {
Bundle b = getIntent().getExtras();
boolean cameFromNotification = b.getBoolean("fromNotification");
}
You can try this in your Notification
Intent intent=new Intent();
intent.setAction("Activity1");
In the Activity override onNewIntent() method and get action so you can determine the activity is called or not.
Better than using the reserved action field of your intent as specified by #ricintech, you could use an extra parameter in your pending intent and detect it in your onCreate method and in your onNewIntent metod inside your activity.

Categories

Resources