[英]How to know if Application onCreate method get called on push notification receive?
如果應用程序由用戶明確啟動,我想向我的服務器發送一些分析事件。
我將分析代碼放在Application的onCreate方法中。 但是問題是當應用程序收到推送通知時,會調用應用程序的on create方法,這是不希望的。
我不想在Activity的onCreate上放置分析調用,因為我根據推送通知進行了多個啟動活動。
有什么方法可以檢測通過推送通知調用Application的onCreate方法嗎?
我認為您可以跟蹤onNewIntent()
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.i(TAG, "PUSH :: ON NEW INTENT");
extras = intent.getExtras();
}
並在通知接收時間創建待定意向。
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.app_logo)
.setContentTitle("Title")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.