我有一个App extends Application
类,它具有onCreate()
函数,该函数在Android应用程序的开头执行
当用户打开应用程序时,以及收到推送通知时,会触发此操作。推送通知代码如下,并遵循此处的标准做法(http://developer.android.com/guide/topics/ui/notifiers/notifications.html)
如何检查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)
);
}
}
答案 0 :(得分:2)
即使这是可能的,也不应该这样做。
当上一个活动结束时,Android可能会也可能不会保留VM。如果它没有杀死VM,那么下次启动应用程序时(无论采用何种方式),Android将(或至少可以)重用相同的_id:"gExyMfiivpb4ZtQTb"
实例,而无需再调用Application
。因此,如果您根据调用onCreate()
时确定的某些条件更改应用的行为,则下次启动时可能会出错。
答案 1 :(得分:0)
你可以在你已经传递的未决意图的意图上添加额外内容,并且如果它们存在或者不存在,可以在你的主要活动上检查相同的.....如果没有你发送的任何内容,那么就按照捆绑数据进行检查用户启动app不推送通知希望通过推送通知帮助其他。