[英]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.