简体   繁体   English

如何检查是否通过推送通知或在用户打开应用程序期间调用了App.onCreate()

[英]How to check if App.onCreate() was called via a push notification or during app open by an user

I have an App extends Application class that has an onCreate() function that is executed at the start of the Android application 我有一个App extends Application类,该类具有一个onCreate()函数,该函数在Android应用程序的开头执行

This gets fired when the user opens the app, and also when a push notification is received. 当用户打开应用程序以及收到推送通知时,将触发此事件。 The push notification code is as below and follows the standard practice here ( http://developer.android.com/guide/topics/ui/notifiers/notifications.html ) 推送通知代码如下,并遵循此处的标准做法( http://developer.android.com/guide/topics/ui/notifiers/notifications.html

How can I check inside the App.onCreate() whether the function was called during a push notification receipt (via the IntentService below) or another way (ie via the user actually opening the app by pressing on the app icon)? 我如何在App.onCreate()内部检查是否在推送通知接收期间(通过下面的IntentService)或其他方式(即通过用户实际按下应用程序图标打开应用程序)调用了该函数?

push notification code 推送通知代码

public class GCMNotificationIntentService extends IntentService {

    public static final int NOTIFICATION_ID = 1;
    public static final String EXTRA_MESSAGE = "message";
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;

    public GCMNotificationIntentService() {
        super("GcmIntentService");
    }

    public static final String TAG = "GCMNotificationIntentService";

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

        // The getMessageType() intent parameter must be the intent you received
        // in your BroadcastReceiver.
        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {        // has effect of unparcelling Bundle

            /*
             * Filter messages based on message type. Since it is likely that GCM
             * will be extended in the future with new message types, just ignore
             * any message types you're not interested in, or that you don't
             * recognize.
             */

            if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
//                sendNotification("Send error: " + extras.toString());
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
//                sendNotification("Deleted messages on server: " + extras.toString());
            }
            // If it's a regular GCM message, process it
            else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {

                // if this is a normal message from Earthmiles backend
                if (extras.containsKey(EXTRA_MESSAGE)){
                    sendNotification("" + extras.get(EXTRA_MESSAGE));
                    Log.i(TAG, "Received Server Push Notif: " + extras.toString());
                } else if(extras.containsKey("mp_message")) {
                    String mp_message = intent.getExtras().getString("mp_message");
                    sendNotification(mp_message);
                    Log.i(TAG, "Received Mixpanel Push Notif: " + mp_message);
                }
            }

        }
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }

    private void sendNotification(String msg) {
        Log.d(TAG, "Preparing to send notification...: " + msg);
        mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.earthmiles_logo_green)
                .setContentTitle("Earthmiles")
                .setSmallIcon(R.drawable.earthmiles_logo_green)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .setContentText(msg);

        mBuilder.setContentIntent(contentIntent);
        mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
        mBuilder.setAutoCancel(true);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
        Log.d(TAG, "Notification sent successfully.");
        App.getAnalytics().track(Emanalytics.RECIEVED_PUSH_NOTIF, new Properties()
                        .putValue("Message Text", msg)
        );

    }
}

Even if this is possible, you shouldn't do it. 即使有可能,也不应该这样做。

When the last activity finishes, Android may or may not keep the VM around. 最后一个活动完成后,Android可能会保留虚拟机,也可能不会保留该虚拟机。 If it doesn't kill the VM, then the next time you launch the app (by whatever means), Android will (or at least may) reuse the same Application instance without another call to onCreate() . 如果它没有杀死VM,那么下次您启动该应用程序(通过任何方式)时,Android将(或至少可以)重用同一Application实例,而无需再次调用onCreate() So if you're changing your app's behavior based on some condition determined at the time onCreate() was called, it might do the wrong thing the next time you start it. 因此,如果您基于调用onCreate()时确定的某种条件来更改应用程序的行为,则下次启动该应用程序时可能会做错事情。

您可以将多余的意图放在已传递的未决意图上,并可以在主要活动中检查是否存在这些意图.....如果没有发送,则按照捆绑包数据进行操作,这意味着应用程序已启动通过用户不推送通知希望通过推送通知对其他方面有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Galaxy Grand Prime-App.onCreate()调用得太频繁了吗? - Galaxy grand prime - App.onCreate() called too often? 单击通知以打开最小化的应用程序时,如何防止被称为“ onCreate”? - How to preven 'onCreate' to be called when clicking on notification to open minimized app? 如何知道应用何时打开(而不是何时调用onCreate)? - How to know when the app is open (and not when onCreate is called)? 用户关闭应用程序时如何启用推送通知,用户打开应用程序时如何禁用推送通知 - How to Enable Push notification when user closes App and disable when user open app android:如何在点击推送通知时打开应用 - android:how to open the app on clicking on push notification 当用户点击推送通知时,在ionic应用程序中打开特定视图 - Open a specific view in ionic app when user taps on push notification 如何知道是否在推送通知上调用Application onCreate方法? - How to know if Application onCreate method get called on push notification receive? Android-收到推送通知后如何检查我的应用程序是否打开? - Android - How to check my app is open when receive a push-notification? 如何在我的应用程序的WebView组件中打开推送通知? - How to open the push notification in the WebView component of my app? Android 应用程序 onCreate 在重新启动期间未调用主屏幕启动器应用程序 - Android Application onCreate not called for homescreen launcher app during reboot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM