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>
This class was deprecated in API level 30. IntentService is subject to all the background execution limits imposed with Android 8.0 (API level 26). Consider using WorkManager or JobIntentService , which uses jobs instead of services when running on Android 8.0 or higher.
First, an Intent instance is generated and any data (like the message text) is packaged in the intent using the extras. Finally, the IntentService is started with a call to startService(). The service takes over from here, catching each intent request, processing it, and shutting itself down when it's all done.
The flags you can use to modify the default behavior are: FLAG_ACTIVITY_NEW_TASK. Start the activity in a new task. If a task is already running for the activity you are now starting, that task is brought to the foreground with its last state restored and the activity receives the new intent in onNewIntent() .
Sending binary content Intent shareIntent = new Intent(); shareIntent. setAction(Intent. ACTION_SEND);
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With