简体   繁体   中英

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. But the problem is when push notification is received by the app, Application's on create method get called, which is undesired.

I dont want to put analytics call in Activity's onCreate because I am having multiple launch activities depending on push notification.

Is there any way to detect that Application's onCreate method get called through push notification?

I think you can track 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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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