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

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

Related

A few questions about the notification

I have a few questions.
1. when I send a notice that it replaces the old.
I need to increment a counter notification.
how to do that when you click on the notification to open certain fragment?
private static void generateNotification(Context context, String message,int type) {
String title = null;
int icon = R.drawable.ic_stat_gcm;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
how to do that when you click on the notification to open certain fragment?
You cannot open Fragment directly. You need to open Activity that then will use that Fragment
Calculate the number if notification come before user last open n and increase the counter by this way.
notification.number += count(here is number of notification);`
Thanks

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

notification disable when move on other activity in android

I have to develop an one push notification application.
i did like :
#Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
regId = GCMRegistrar.getRegistrationId(this);
String message = intent.getExtras().getString("offer");
displayMessage(context, message);
// notifies user
if(Constants.response != null){
generateNotification(context,message);
}
}
private static void generateNotification(Context context,String message) {
int icon = R.drawable.icon;
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);
Intent notificationIntent = new Intent(context, MyDealProducts.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
here the notification is generated successfully on logged in customer.
Now didn't visit these notification at the same time logged out the customer form which means i don't need to enable the notification.
But here the notification is enabled.How can i disable the notification after logged out the customer ???
please give me a suggestions ???
For this you can cancel the Notification that you have generated, whenever the user is logged out.
Use the method cancel(int id) (Where id is the notification id, in your case, its 0) or cancelAll() of NotificationManager.

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.

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