Notification passes old Intent Extras when the Activity that read the extras values is on top
using the answser of CommonsWare that helped with the override onNewIntetn and the link:
http://www.helloandroid.com/tutorials/communicating-between-running-activities
1 into xmlFile: put th android:launchMode="singleTask"
for the activity will receive the extra parameters with getExtras.
<activity android:name=".ActivityWillReceiveWithGetExtras"
android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true">
</activity>
2.into the activity you that will receive the values with get_extras(...) override a method called onNewIntent:
2.1 Observation: put the line:
setIntent(intent);
to set the identifier of the intent.
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);//must store the new intent unless getIntent() will return the old one
getExtraParameterActual();
}
2.2 get the Extra parameters into a function that will have inside the command getExtras(...
getExtraParameterActual();
Write the function of the top getExtraParameterActual();
private void getExtraParameterActual() {
Intent intent = getIntent();//take back the value set with //setintenT of pass 2.1
user = getIntent().getExtras().getString(USER);//
}
5.
into OnCreate() call the e getExtraParameterActual();
and if necessary reload your views with a method for example reloadMyViews()
- into onResume() reload your views again with the same function of the pass 5 reloadMyViews()
7 the notificatio code I used take care with the FLAGS
public void notificationCreateGu(String User) {
Log.d(TAG,nameOfTheService+"BUG createnotification for received CHAT messages useruidOfTheFriendNear="+newMessageUserUidOfSender);
Intent it = new Intent(this,ActivityWillReceiveWithGetExtras.class);
it.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
it.putExtra(USER,user);
StoreValuesClass.count=StoreValuesClass.count+2;
PendingIntent pi = PendingIntent.getActivity(this, StoreValuesClass.count,it, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this)
.setTicker(newMessageUserUidOfSender )
.setSmallIcon(android.R.mipmap.sym_def_app_icon)
.setContentTitle("Title Message ")
.setContentText(String.valueOf(newMessageUserUidOfSender))
.setContentIntent(pi)
.setAutoCancel(true)
.build();
int m;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
m= StoreValuesClass.count=StoreValuesClass.count+2;
notificationManager.notify((m), notification);
}
Notification passes old Intent Extras
You are sending the same request code for your pending intens.
Change this:
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
To:
PendingIntent contentIntent = PendingIntent.getActivity(context, UNIQUE_INT_PER_CALL, notificationIntent, 0);
intents are not created if you send the same params. They are reused.
Android Status Bar Notifications - Intent getting the old extras on the second time
You need to set up a clean flag to PendingIntent
:
PendingIntent pintent =
PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
Have a look at this post that gives a longer explanation.
Notification PendingIntent extras always empty
So I finally found the problem. After debugging several flag options and changing request codes. The request code here is irrelevant to the problem.
Responsible for the empty (not null) bundle is the flag ACTIVITY_NEW_TASK.
If I went with all other tested flags for the Intent or the PendingIntent everything works fine.
The android doc says following for ACTIVITY_NEW_TASK:
When using this flag, if a task is already running for the activity
you are now starting, then a new activity will not be started;
instead, the current task will simply be brought to the front of the
screen with the state it was last in. See FLAG_ACTIVITY_MULTIPLE_TASK
for a flag to disable this behavior.
This seems to indicate that the old Intent is still present and not the new one which I build in the receiver. Funny fact: even if I closed the app and then started the PendingIntent the bundle was empty. The doc as for my understanding would state it otherwise (activity not in a task? -> new task with new activity).
Pending intent seems to not pass extras
Try to use PendingIntent.FLAG_UPDATE_CURRENT
.
Also add Intent.FLAG_ACTIVITY_SINGLE_TOP
so that if your activity is already opened, it is not recreated.
Add the above 2 flags to your intent.
Also in your DashboardActivity implement onNewIntent
@Override
public void onNewIntent(Intent intent){
Bundle extras = intent.getExtras();
if(extras != null){
if(extras.containsKey("pending"))
{
}
}
Unable to retrieve new intent extras
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));
Related Topics
Android API 23 Change Navigation View Headerlayout Textview
Difference Between Getview & Getdropdownview in Spinneradapter
How to Use Percentage for Android Layout
How to Get Pid of Android Application Without Using Adb Shell
How to Play an Audio File on a Voice Call in Android
Gradle: Add Dependency for a Specific Flavour of The Library
Custom Attributes in Android Fragments
When Should I Do Certain Sqlite Operations on Another Thread(Not The Main Thread)
Avd - Panic: Could Not Open... - Not a Path Issue
How to Enable Cookies in Android Webview
Android, How Do How to Get a List of All Files in a Folder
How to Find Memory Leaks from Native Code in Android
Gradle Project Sync Failing After Google Announced The New Sdk Versioning System
Stuck with Gradle Build Running
How to Get a List of Android Permissions
Ionic Cordova App Stopped Compiling After Google's June 17Th Firebase Sdk Update