简体   繁体   English

如何知道是否在推送通知上调用Application onCreate方法?

[英]How to know if Application onCreate method get called on push notification receive?

I want to send some analytics event to my server if application is started by the user explicitly. 如果应用程序由用户明确启动,我想向我的服务器发送一些分析事件。

I placed analytics code in Application's onCreate method. 我将分析代码放在Application的onCreate方法中。 But the problem is when push notification is received by the app, Application's on create method get called, which is undesired. 但是问题是当应用程序收到推送通知时,会调用应用程序的on create方法,这是不希望的。

I dont want to put analytics call in Activity's onCreate because I am having multiple launch activities depending on push notification. 我不想在Activity的onCreate上放置分析调用,因为我根据推送通知进行了多个启动活动。

Is there any way to detect that Application's onCreate method get called through push notification? 有什么方法可以检测通过推送通知调用Application的onCreate方法吗?

I think you can track onNewIntent() 我认为您可以跟踪onNewIntent()

 @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.i(TAG, "PUSH :: ON NEW INTENT");
        extras = intent.getExtras();
    }

and create a pending intent while notification received time. 并在通知接收时间创建待定意向。

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM