How to Pass data with Notification to Main Activity - android

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.

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

notification can't go current activity

In my notification, I want to start current activity when click in pending intent. In google, many ways found and I tried many times, it didn't work. here is my code,
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
String startTime = intent.getStringExtra("Strar_time");
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
long when = System.currentTimeMillis(); // notification time
Notification notification = new Notification(R.drawable.ic_launcher, "reminder", when);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= notification.FLAG_AUTO_CANCEL;
notification.vibrate = new long[]{100, 200, 100, 500};
notification.defaults = Notification.DEFAULT_ALL;
Intent notificationIntent = new Intent(this, Goo.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP/* | Intent.FLAG_ACTIVITY_SINGLE_TOP*/);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent , 0);
notification.setLatestEventInfo(getApplicationContext(), "It's about time", "Your time is "+startTime, contentIntent);
notification.contentIntent=contentIntent;
nm.notify(NOTIF_ID, notification);
NOTIF_ID++;
Log.i("NotiID",NOTIF_ID+"");
}
Try this function given below,
private static void generateNotification(Context context, String message) {
int icon = R.drawable.launch_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 = null;
notificationIntent = new Intent(context, Main.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
Only one working way I found so far is to make a dummy activity, start it and finish it in onRusume(). However this technique only bring application to front and do not start application when it is closed.
Then I used following workaround to bring application on front if it is alive in background otherwise start it.
Manifest:
<application>
.
.
.
<activity
android:name=".DummyActivity"
/>
.
.
</application>
In code:
public class DummyActivity{
public static boolean isAppAlive;
#Override
protected void onResume() {
// start application if it is not alive
if(!isAppAlive){
PackageManager pmi = getPackageManager();
Intent intent = pmi.getLaunchIntentForPackage(Your applications package name);
if (intent != null){
startActivity(intent);
}
}
finish();
}
In your main activity's onCreateMethod.
#Override
public void onCreate(Bundle savedInstanceState) {
DummyActivity.isAppAlive = true;
}
It is obvious that when your application is fully closed, isAppAlive will restore to its default value which is false. Thus application will be start.

how to know if the app was launched by clicking on the push notification

I want to know if there is a flag and parameter that can tell me if the user launched the activity/app by clicking on the push notification in the notification tray.
My code in C2DMReceiver.java
Context context = getApplicationContext();
PackageManager manager = context.getPackageManager();
Intent notificationIntent = manager
.getLaunchIntentForPackage(packageName);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
**notificationIntent.putExtra("fromRemote", true);**
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification();
notification.icon = R.drawable.icon;
notification.tickerText = message;
notification.number = badge;
notification.when = System.currentTimeMillis();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults = Notification.DEFAULT_ALL;
notification.setLatestEventInfo(context, "Draw Something", message,
pendingIntent);
notification.contentIntent = pendingIntent;
notificationManager.notify(0, notification);
I tried setting
notificationIntent.putExtra("fromRemote", true);
but when the app was launched there were no extras in the intent.
my code in the onCreate function of my mainactivity
Bundle extras = getIntent().getExtras();
if (extras != null) {
print("onCreate - bundle has extras");
for (String key: extras.keySet())
{
Log.v ("mytag", "MainActivity onCreate key =" + key);
}
}
else {
Log.v ("mytag", "onCreate - bundle has NO extras");
}
The output i get is onCreate - bundle has NO extras. So the extras are not getting passed through.
So is there any other way?? It is so easy in iOS
Just posting an answer so that people who visit this page get the solution.
You need to use putExtra(ID_KEY,id) when you create your Intent for starting your application, and in your onCreate() method you can use getIntent().getExtras().getInt(ID_KEY); to retrieve your passed id integer.
The passed extra could be anything, a boolean, String etc.
Source - https://stackoverflow.com/a/7358692/3036759

Android Status Bar Notifications - Intent getting the old extras on the second time

When I create a notification that is sent through C2DM and received in my app I want to pass some of the data that came with the push notification from C2DM in the intent extras. This works fine the first time I open my notification. Then the data is received with onNewIntent or in onCreate depending on the state of the activity.
But if I send a second push notification through C2DM it is received correctly with the new data but when getting the extras from the intent I still get the data from the previous message. The title and the data I want to see is shown correctly in the notification. So something must be wrong the my intent. This happens if the activity is running and if it isn't.
To create the notification I do the following:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_stat_notify_push, "Message received", System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notification.defaults |= Notification.DEFAULT_LIGHTS;
Intent intent = new Intent(context, DesktopApp.class);
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("msg_id", msg_id);
intent.putExtra("title", title);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
notification.setLatestEventInfo(context, "New message", title + String.valueOf(msg_id), pendingIntent);
notificationManager.notify(0, notification);
To then read the intents:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent myIntent = getIntent(); // this is just for example purpose
int i = myIntent.getIntExtra("msg_id", -1);
if (i != -1) Toast.makeText(this, "Got message! " + String.valueOf(i), Toast.LENGTH_LONG).show();
}
#Override
public void onNewIntent(Intent intent){
super.onNewIntent(intent);
Bundle extras = intent.getExtras();
if (extras != null) {
int i = extras.getInt("msg_id", -1);
if (i != -1) Toast.makeText(this, "Got message! " + String.valueOf(i), Toast.LENGTH_LONG).show();
}
}
Any suggestions?
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.
Use this
PendingIntent pi = PendingIntent.getActivity(context, UNIQUE_ID,
intent, 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.

Categories

Resources