如何知道是否在推送通知接收时调用了Application onCreate方法?

时间:2016-02-19 06:56:42

标签: android

如果用户明确启动了应用程序,我想将一些分析事件发送到我的服务器。

我在App的onCreate方法中放置了分析代码。但问题是当应用程序收到推送通知时,应用程序的创建方法被调用,这是不受欢迎的。

我不想将分析调用放在Activity的onCreate中,因为我根据推送通知进行了多次启动活动。

有没有办法检测通过推送通知调用Application的onCreate方法?

1 个答案:

答案 0 :(得分:0)

我认为你可以跟踪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());