Notification getIntent().getExtras() gets the same bundle - android

The problem im having is I call sendAffimationNotice() twice passing a unique aff_id each time (as confirmed by printing "putting " + aff_id).
This prints 'putting 1' and on the second call 'putting 2'.
The phone now has 2 notifications. Though when both are clicked they both have the same ID toasted in the onCreate method on the intents activity.
Both times it prints "ID is: 1" even though both notifications are unique.
public class PossAffNotification{
private static int notif_ID = 1;
private static NotificationManager notificationManager;
private static Notification note;
private static PendingIntent contentIntent;
public static void sendAffimationNotice(Context ctx, String title,String contentText,int aff_id){
notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
note = new Notification(android.R.drawable.btn_star_big_on, contentText, System.currentTimeMillis() );
note.defaults |= Notification.DEFAULT_SOUND;
note.defaults |= Notification.DEFAULT_VIBRATE;
note.defaults |= Notification.FLAG_AUTO_CANCEL;
note.flags |= Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(ctx, com.mindfsck.PossAff.MainActivity.class);
notificationIntent.putExtra("aff_id",aff_id);
System.out.println("putting " + aff_id);
notificationIntent.setAction("com.mindfsck.PossAff.intent.action.aff");
notificationIntent = notificationIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, 0);
note.setLatestEventInfo(ctx, title, contentText, contentIntent);
notificationManager.notify(notif_ID,note);
notif_ID += 1;
}
};
public class MainActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Intent serviceIntent = new Intent(this,PAService.class);
this.startService(serviceIntent);
Bundle extras = getIntent().getExtras();
if(extras !=null) {
Toast.makeText(getApplicationContext(), "ID is: " + extras.getInt("aff_id"),
Toast.LENGTH_SHORT).show();
}
}
}

Your Intent is the same on all non-extra parameters (e.g., action). Hence, you get the same PendingIntent back from your getActivity() call, as there is only one PendingIntent per distinct Intent. You need to change something -- beyond extras -- in your second Intent, such as a different action string.

The problem for this issue came from this line:
contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, 0);
The last parameter shouldn't be 0 it should be PendingIntent.FLAG_CANCEL_CURRENT
contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
otherwise in notificationIntent you will receive same data all the time

This article of mine may be helpful here: "A pitfal in PendingIntent"
The issue basically comes from the fact, that the system does not assume that only because we pass a new Intent object to the pending intent, we want this new intent object to be delivered and just keeps the old (initial) pending intent alive.
To get the desired semantics, we need to pass a flag to tell the system:
PendingIntent pintent =
PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);

Related

Android push message issue, getting old message always on display screen

please help me out. I am stuck with GCM push message.
Everything working perfect but when I am trying to display my message on next screen I am getting always old one or first one.
But If I check my log cat, I am getting new message always from the server. So where is problem I am not getting. I have tried many code on stackoverflow. Here is the snippet of my code-
// this is the my service class
public class GCMIntentService extends GCMBaseIntentService {
public GCMIntentService() {
super(SENDER_ID);
}
// this is the onMessage revive method
#Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "new message= ");
String message = intent.getExtras().getString("message");
generateNotification(context, message);
System.out.println(message+"++++++++++1");
}
// this is the notification method
private void generateNotification(Context context, String message) {
System.out.println(message+"++++++++++2");
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
String subTitle = "Important News for you!";
Intent notificationIntent = new Intent(context, NotificationView.class);
notificationIntent.putExtra("content", message);
//PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//PendingIntent intent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
notification.setLatestEventInfo(context, title, subTitle, intent);
//To play the default sound with your notification:
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
public class NotificationView extends Activity {
// and this is the my next activity where i am displaying push message-
Intent intent=getIntent();
stringValue=intent.getStringExtra("content");
System.out.println(stringValue);
I have already try-
PendingIntent intent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
and this-
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Both did not work for me.
Code is fine ,Try using String Buffer may because String Data cant be change unless new is specified every time im new here give me vote up :)

Bundle data is not updating in the activity passed from a notification in service, always populating the first bundle

I am handling push notification in my application using GCMIntentService. I am creating a status bar notification and navigating to an activity using pending Intent.
my Code for creating a notification is :
private static void generateNotification(Context context, String message, String title1, String desc, String soln, String date, String time) {
int icon = R.drawable.notification_icon;
long when = System.currentTimeMillis();
Log.i("","###### notificatin"+title1+desc+soln+message);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context,AppLog.class);
notificationIntent.putExtra(Description.REQUEST_FROM, "notification");
notificationIntent.putExtra(Description.INFO_TITLE, title1);
notificationIntent.putExtra(Description.INFO_DESC, desc);
notificationIntent.putExtra(Description.INFO_SOLUTION, soln);
notificationIntent.putExtra(Description.INFO_DATE, date);
notificationIntent.putExtra(Description.INFO_TIME, time);
PendingIntent pIntent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, pIntent);
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notification.sound=uri;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
And inside my AppLog.class I am handling it like :
if(bundle!=null && bundle.containsKey(Description.REQUEST_FROM)){
Log.i("","###### applog"+bundle);
}
When the first notification is sent to the device data will be populated correctly in my AppLog Activity class. But onwards for all notifications it always show me the old bundle.
I tried everything but still the issue persist. Is there any issue with pending intents or notification created from services ?
This works
PendingIntent pIntent = PendingIntent.getActivity(context, 0,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Instead of always passing a 0 value as the second parameter try and pass a random number.
Random r = new Random();
int no = r.nextInt(999999);
PendingIntent pIntent = PendingIntent.getActivity(context, no,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

How to Pass data with Notification to Main Activity

Am using phonegap 2.5.0 for my android app. In this am enabling notification with intent. The intent having some extra data. The notification code is below,
Notification notification = new Notification(icon, contentTitle, when);
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.putExtra("notificationType", "updateAvailable");
notificationIntent.putExtra("updateUrl", updateUrl);
notificationIntent.putExtra("updateVersion", updateVersion);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
int requestID = (int) System.currentTimeMillis();
PendingIntent contentIntent = PendingIntent.getActivity(this, requestID, notificationIntent, 0);
//PendingIntent contentIntent = PendingIntent.getActivity(this, requestID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);
NotificationManager nm = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(1, notification);
The above generated notification calling the mainactivity of the android app. The MainActivity script is below,
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String notificationType = intent.getStringExtra("notificationType");
if(extras != null)
{
if(extras.getString("notificationType").equals("updateAvailable"))
{
String updateUrl = extras.getString("updateUrl");
String updateVersion = extras.getString("updateVersion");
super.loadUrl("file:///android_asset/www/update.html?updateVersion="+updateVersion+"&updateUrl="+updateUrl);
}else
startMyApp();
}else
{
startMyApp();
}
}
The startMyApp function having code to start application (only called while opening the application directly using application icon).
But the code not working. Application not working (i.e application not opened on clicking the application icon) Unfortunately stopped Application error displayed and application was closed. If am removing intent receiving contents from oncreate function and if only the startMyApp() function called from onCreate application started successfully.
Help me to pass data with notification to MainActivity. The startMyApp()'s code is below
public void startMyApp()
{
super.loadUrl("file:///android_asset/www/dashboard.html");
}
super.loadUrl("file:///android_asset/www/update.html?updateVersion="+updateVersion+"&updateUrl="+updateUrl);
This will not work, since it will search for a file named update.html?updateVersion=xxx&updateUrl=xxx and not update.html.

Passing data from onReceive to new Activity

I've a notification bar, when I click on it, starts a new activity with same data (a string array). I have a problem passing data from onReceive.class to new Activity (Notify). When I click I see new activity but crashes with nullPointerE.
not1[x] is a array of string
My code
AlarmReceiver:
Intent notificationIntent = new Intent("com.example.myapp", null, context, Notify.class);
notificationIntent.putExtra("notify", not1[x]);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Notify:
Bundle b = getIntent().getExtras();
if (b != null) {
Intent intent = getIntent();
String[] notify2 = b.getStringArray("notify");
textView1.setText("notify2");
Try this
String[] getArray=getIntent().getStringArrayExtra("notify");
In between not1[x] is a string not an array.
private NotificationManager mNotificationManager;
private int SIMPLE_NOTFICATION_ID;
mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notifyDetails = new Notification(R.drawable.somepngfile,"some text",System.currentTimeMillis());
Context context = getApplicationContext();
CharSequence contentTitle = "title";
CharSequence contentText = "ontent";
Intent notifyIntent = new Intent(thisclass.this,nextclass.class);
notifyIntent.putExtra("Value", finalresponse[1]+","+finalresponse[2]+","+finalresponse[3]);
PendingIntent intent =
PendingIntent.getActivity(BackService.this, 0,
notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
notifyDetails.defaults|= Notification.DEFAULT_SOUND;
notifyDetails.defaults|= Notification.DEFAULT_LIGHTS;
notifyDetails.defaults|= Notification.DEFAULT_VIBRATE;
notifyDetails.defaults|= Notification.FLAG_AUTO_CANCEL;
notifyDetails.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
In next class
Intent i = getIntent();
i.getStringExtra("Value");
String[] response=i.getStringExtra("Value").split(",");
Is this not1[x] a String Array? It looks like an element of a String Array that is actually a String.
Just replace below code in AlarmReceiver class. its works for me...
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
if we use 0 instead of PendingIntent.FLAG_UPDATE_CURRENT flag, on the Receiver side i.e getIntent().getExtras() not update the data dynamically, assign 1st passed data every time....

Android notification with pending Intent

I am sending notification depending on some logic.
public class DbAdapter_Assignment extends DbAdapter {
public DbAdapter_Assignment(Context context) {
super(context);
}
public void deleteUnassignedTask(Assignment[] assignments)
{
//some logic
sendNotification(String a. String b);
}
private void triggerNotification(String s, String id)
{
CharSequence title = "TASK UNASSIGNED";
CharSequence message = s;
NotificationManager notificationManager;
notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.checkno, s, System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
// No. 1 solution
PendingIntent pendingIntent = PendingIntent.getActivity(context, Integer.parseInt(id), null, PendingIntent.FLAG_UPDATE_CURRENT);
// -----------
// No. 2 solution
Intent notificationIntent = new Intent(context, MyTask.class);
notificationIntent.putExtra("EmpID", Functions.getLoginEmpID(context));
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP );
PendingIntent pendingIntent = PendingIntent.getActivity(context,
Integer.parseInt(id), notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// ----------------
notification.setLatestEventInfo(context, title, message, pendingIntent);
notificationManager.notify(Integer.parseInt(id), notification);
}
}
No. 1 solution is working. the thing I want to add is if click on that notification, go to MyTask class. So I tried No. 2 solution and IT"S NOT WORKING for me. It doesn't show any notification if do no. 2 solution. What am I missing? Is ir because of the class is not an activity or servise ?
Your Notification looks right to me.
I haven't tried to create a Notification outside of a class that extends Activity, but I would assume if you can access the NotificationManager then you should be able to do it.
Does the class you're pointing the PendingIntent to extend Activity?
Could there be a problem in the Activity that your PendingIntent is using?
Is that Activity in the AndroidManifest.xml?

Categories

Resources