[英]How to check if App.onCreate() was called via a push notification or during app open by an user
I have an App extends Application
class that has an onCreate()
function that is executed at the start of the Android application 我有一个
App extends Application
类,该类具有一个onCreate()
函数,该函数在Android应用程序的开头执行
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 )
推送通知代码如下,并遵循此处的标准做法( 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 (ie via the user actually opening the app by pressing on the app icon)? 我如何在App.onCreate()内部检查是否在推送通知接收期间(通过下面的IntentService)或其他方式(即通过用户实际按下应用程序图标打开应用程序)调用了该函数?
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. 最后一个活动完成后,Android可能会保留虚拟机,也可能不会保留该虚拟机。 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()
. 如果它没有杀死VM,那么下次您启动该应用程序(通过任何方式)时,Android将(或至少可以)重用同一
Application
实例,而无需再次调用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. 因此,如果您基于调用
onCreate()
时确定的某种条件来更改应用程序的行为,则下次启动该应用程序时可能会做错事情。
您可以将多余的意图放在已传递的未决意图上,并可以在主要活动中检查是否存在这些意图.....如果没有发送,则按照捆绑包数据进行操作,这意味着应用程序已启动通过用户不推送通知希望通过推送通知对其他方面有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.