Reputation: 405
I have a problem. When i call finish()
method activity still hold in task-manager, and if user restart it from task-manager my activity receive old intent. If that intent was sent from push-notification I have unwanted reaction: my app start process intent with push-notification data.
How correctly manage push-notification intent behavior in my activity for avoid wrong activity state?
My app receive a push-notification and form pending intent for reaction on push:
final NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int defaultOptions = Notification.DEFAULT_LIGHTS;
defaultOptions |= Notification.DEFAULT_SOUND;
Intent intentAction = new Intent(context, MainActivity.class);
intentAction.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intentAction.putExtra(BUNDLE_COLLAPSE_KEY, data.getString(BUNDLE_COLLAPSE_KEY));
intentAction.setAction(CUSTOM_ACTION);
final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intentAction, PendingIntent.FLAG_UPDATE_CURRENT);
int notificationFlags = Notification.FLAG_AUTO_CANCEL;
final Notification notification = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.icon_splash)
.setContentTitle(context.getResources().getString(R.string.app_name))
.setContentText(data.getString(BUNDLE_PUSH_CONTENT_DATA))
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setDefaults(defaultOptions)
.getNotification();
notification.flags |= notificationFlags;
mNotificationManager.notify(NOTIFICATION_ID, notification);
after user came in app from push, application, obviously, receive intent with CUSTOM_ACTION
and do some work:
private void intentProcess(Intent intent) {
boolean customAction = intent.getAction().equals(GCMPushReceiver.CUSTOM_ACTION);
if (customAction) {
//push reaction, do some staff with intent
} else {
//no push reaction, user just open activity
}
}
I call intentProcess
method from onCreate
and from onNewIntent
:
public class MainActivity extends FragmentActivity {
//this case if my app closed and user tap on push
@Override
protected void onCreate(Bundle savedInstanceState) {
/* ... */
intentProcess(getIntent());
}
//this case if my app opened and user tap on push
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
intentProcess(intent);
}
}
Activity declaration in manifest:
<activity
android:name=".ui.activity.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Upvotes: 11
Views: 888
Reputation: 1127
Try to Use this
int id= NotificationID.getID();
final PendingIntent pendingIntent = PendingIntent.getActivity(context, id, intentAction, PendingIntent.FLAG_UPDATE_CURRENT);
// rest of your code
notificationManager.notify(id, notification);
Add a new class
public class NotificationID {
/**
* An AtomicInteger is used in applications such as atomically incremented
* counters, and cannot be used as a replacement for an Integer. However,
* this class does extend Number to allow uniform access by tools and
* utilities that deal with numerically-based classes.
*/
private final static AtomicInteger c = new AtomicInteger(0);
/**
* Method to the automatically incremented int value.
*
* @return
*/
public static int getID() {
return c.incrementAndGet();
}
}
Every time the notification generated it creates new intent with different id.
If you don't want your activity display/added to task manager add these lines in activity tag of AndroidManifest.
android:excludeFromRecents="true"
android:noHistory="true"
Upvotes: 1