How to check if App.onCreate() was called via a push notification or during app open by an user

1.7k views Asked by At

I have an App extends Application class that has an onCreate() function that is executed at the start of the Android application

This gets fired when the user opens the app, and also when a push notification is received. The push notification code is as below and follows the standard practice here (http://developer.android.com/guide/topics/ui/notifiers/notifications.html)

How can I check inside the App.onCreate() whether the function was called during a push notification receipt (via the IntentService below) or another way (i.e. via the user actually opening the app by pressing on the app icon)?

push notification code

public class GCMNotificationIntentService extends IntentService {

    public static final int NOTIFICATION_ID = 1;
    public static final String EXTRA_MESSAGE = "message";
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;

    public GCMNotificationIntentService() {
        super("GcmIntentService");
    }

    public static final String TAG = "GCMNotificationIntentService";

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

        // The getMessageType() intent parameter must be the intent you received
        // in your BroadcastReceiver.
        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {        // has effect of unparcelling Bundle

            /*
             * Filter messages based on message type. Since it is likely that GCM
             * will be extended in the future with new message types, just ignore
             * any message types you're not interested in, or that you don't
             * recognize.
             */

            if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
//                sendNotification("Send error: " + extras.toString());
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
//                sendNotification("Deleted messages on server: " + extras.toString());
            }
            // If it's a regular GCM message, process it
            else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {

                // if this is a normal message from Earthmiles backend
                if (extras.containsKey(EXTRA_MESSAGE)){
                    sendNotification("" + extras.get(EXTRA_MESSAGE));
                    Log.i(TAG, "Received Server Push Notif: " + extras.toString());
                } else if(extras.containsKey("mp_message")) {
                    String mp_message = intent.getExtras().getString("mp_message");
                    sendNotification(mp_message);
                    Log.i(TAG, "Received Mixpanel Push Notif: " + mp_message);
                }
            }

        }
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }

    private void sendNotification(String msg) {
        Log.d(TAG, "Preparing to send notification...: " + msg);
        mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.earthmiles_logo_green)
                .setContentTitle("Earthmiles")
                .setSmallIcon(R.drawable.earthmiles_logo_green)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .setContentText(msg);

        mBuilder.setContentIntent(contentIntent);
        mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
        mBuilder.setAutoCancel(true);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
        Log.d(TAG, "Notification sent successfully.");
        App.getAnalytics().track(Emanalytics.RECIEVED_PUSH_NOTIF, new Properties()
                        .putValue("Message Text", msg)
        );

    }
}
2

There are 2 answers

0
Kevin Krumwiede On

Even if this is possible, you shouldn't do it.

When the last activity finishes, Android may or may not keep the VM around. If it doesn't kill the VM, then the next time you launch the app (by whatever means), Android will (or at least may) reuse the same Application instance without another call to onCreate(). So if you're changing your app's behavior based on some condition determined at the time onCreate() was called, it might do the wrong thing the next time you start it.

0
Vivek Khare On

You can put extras on intent that you have passed on pending intent and can check for same on your main activity if they exist or not..... then do as per bundle data if nothing u send is on it that means app was started by user not push notification hope that helps else by push notification.