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)
);
}
}
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 toonCreate()
. So if you're changing your app's behavior based on some condition determined at the timeonCreate()
was called, it might do the wrong thing the next time you start it.