KVISH
KVISH

Reputation: 13178

GCM message launching an Intent

Right now I am creating a notification intent as per the following:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, MyActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.setAction(Long.toString(System.currentTimeMillis()));

notificationIntent.putExtra("key", value);

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Notification updateComplete = new NotificationCompat.Builder(context)
    .setContentTitle(title)
    .setContentText(msg)
    .setTicker(title)
    .setWhen(System.currentTimeMillis())
    .setContentIntent(contentIntent)
    .setDefaults(Notification.DEFAULT_SOUND)
    .setAutoCancel(true)
    .setSmallIcon(R.drawable.icon_notifications)
    .build();

notificationManager.notify(100, updateComplete);

When the app is already in the background, everything works. The function onNewIntent is called and I get the value from the notificationIntent extras. However, if the app is NOT in the background, it goes to the root activity (login screen) which forwards the user to MyActivity. But the root activity doesn't get the call to onNewIntent, and by the time the user gets to MyActivity, the extras are lost.

Is there anyway around this?

I have been trying to store values elsewhere to no avail... This includes shared preferences.

Upvotes: 0

Views: 443

Answers (4)

KVISH
KVISH

Reputation: 13178

I found out how to do this.

Basically, if the activity you are launching with notificationIntent is already open (if it's the current activity the user is on), it will call onNewIntent(Intent intent). If it is not currently the active activity or if the application is not running, it will launch the activity and go to onCreate().

You have to get the intent from onCreate() by calling getIntent() and checking to see it's not null. Case closed.

Upvotes: 0

dipali
dipali

Reputation: 11188

//this code is put in your gcmservice

Intent intent2 = new Intent();
                intent2.setAction("RECEIVE_MESSAGE_ACTION_NEW");
                intent2.putExtra("senderNum", senderNum);
                intent2.putExtra("verificationCode",
                        ReturnValidationcode(message));
                context.sendBroadcast(intent2);

Upvotes: 1

dipali
dipali

Reputation: 11188

@Override
    protected void onMessage(final Context ctx, Intent intent) {
if (CommonMethod.isAppicationRunning(ctx)) {
            // Creates an explicit intent for an Activity in your app
            Intent resultIntent = new Intent(ctx, ViewMessageDialog.class);
            resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            resultIntent.putExtra("gcmmessage", message);
            ctx.startActivity(resultIntent);
        } else {

            try {
                sendNotification(message);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    }

////////////////////////////////////////////////////////////////////////////

public static boolean isAppicationRunning(Context activity) {
        ActivityManager am = (ActivityManager) activity
                .getSystemService(EGLifeStyleApplication.mContext.ACTIVITY_SERVICE);

        // get the info from the currently running task
        List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
        ComponentName componentInfo = taskInfo.get(0).topActivity;
        Log.i("current activity", "activity"
                + activity.getClass().getSimpleName());

        if (componentInfo.getPackageName().equals(
                EGLifeStyleApplication.mContext.getPackageName())) {
            return true;
        }
        return false;
    }

////////////////////////////////////////////////////////////////////////////////////

private void sendNotification(String message) throws JSONException {
        // this
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

        int icon = R.drawable.app_icon;
        CharSequence tickerText = message; // ticker-text
        long when = System.currentTimeMillis();
        Context context = getApplicationContext();
        context.getClass().getSimpleName();
        Log.i("context.getClass().getSimpleName()",
                "context.getClass().getSimpleName()="
                        + context.getClass().getSimpleName());

        CharSequence contentTitle = "Product Received.";
        CharSequence contentText = message;
        Intent notificationIntent = null;
        int notificationID = CommonVariable.notificationID;

        notificationIntent = new Intent(this, ViewHomeScreen.class);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);
        Notification notification = new Notification(icon, tickerText, when);
        // Play default notification sound
        notification.defaults |= Notification.DEFAULT_ALL;
        notification.setLatestEventInfo(context, contentTitle, contentText,
                contentIntent);
        mNotificationManager.notify(notificationID, notification);
    }

check app running or not?if app is running in forgroung then please call only intent to pass that specific activity.and if app is running in backgroung then call sendnotification() to send notification on notification bar.

Upvotes: 1

Vikas B L
Vikas B L

Reputation: 397

Check this out. I don't know why your activity should run to get the upadate. If you follow that tutorial, then the mobile will automatically get push notification, if there are any. No need to run activity on background.

Upvotes: 0

Related Questions