Passing data from onReceive to new Activity - android

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....

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);

Launch App from Notification with data

I'm trying to use C2DM to trigger a push notification on my android phone, and have the user click the notification and launch a certain activity in the app. How can I pass this information along?
Currently I'm doing the following:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager
= (NotificationManager) getSystemService(ns);
int icon = R.drawable.notification23;
Notification notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_AUTO_CANCEL;
Context appContext = getApplicationContext();
Intent notificationIntent = new Intent(this, RequestDialogActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.putExtra(LocationHandler.KEY_ORIGINATOR, number);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(appContext, contentTitle, contentText, contentIntent);
final int id = 1;
mNotificationManager.notify(id, notification);
I'm trying to store state by calling putExtra on the notificationIntent, but the new activity always has a null savedInstanceState Bundle. How can I pass information along?
the extras you set in the intent have to be retrieved by the getIntent().getExtras() the savedInsanceState is more about retrieve the state when the application have been closed by the system and you want to recover the same state that user saw last time.

How to send 2 strings using Notification?

I'm trying to send 2 strings using Notification, but I don't know how should I do it. I used putExtra for my strings, but on the other activity I get null. I don't know what to do on the other activity. I read about this on net, but I didn't undersand. Can anyone help me?
Here is my code :
private void setNotifiy(){
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence NotificationTicket = "You have a notification";
//CharSequence NotificationContent = ;
CharSequence contentTitle = "You are close to "+name_shop+"!!!";
CharSequence contentText ="So,you can go for shopping:)";
long when = System.currentTimeMillis();
Notification notification = new Notification(R.drawable.icon,
NotificationTicket, when);
Context context = getApplicationContext();
Intent notificationIntent = new Intent(this, ShopsOnMap.class);
notificationIntent.putExtra("latshop",lat_choose);
notificationIntent.putExtra("longshop", long_choose);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
}
In the class ShopsOnMap I wrote just this :
String q = getIntent().getStringExtra("latshop");
System.out.println("!!"+q+"$$");
and I obtain q=null.
I need a simple example, or any idea that could help me. Thanks.
Use FLAG_UPDATE_CURRENT in your getActivity() call, instead of 0, for the fourth parameter, so your new Intent extras are applied to the PendingIntent.

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

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);

Categories

Resources