【问题标题】:C2DM- detecting how application is launchedC2DM - 检测应用程序的启动方式
【发布时间】:2013-03-11 18:28:44
【问题描述】:

所以我们有一个 iOS 应用程序和一个 Android 应用程序,每个应用程序都使用各自的通知方法框架......iOS 有推送,Android 有 C2DM(直到我们将它带到 GCM)......在 iOS 上一切都很好,但是我正在寻找一种方法来检测应用程序是否通过单击 C2DM 消息启动(类似于 iOS 上的 didFinishLaunchingWithOptions 的功能)。

目前,当在 Android 上收到推送消息时,我会根据消息有效负载中包含的数据执行我需要执行的任何处理...因此,当用户启动应用程序时,他们的体验取决于其中的内容该推送消息。无论他们是通过按下主屏幕/历史记录上的图标还是推送消息来启动,情况都是如此。理想情况下,我们希望仅当他们选择该消息时才会发生这种情况,并且如果他们从主/历史屏幕中选择应用程序,那么它应该正常启动。

【问题讨论】:

    标签: android android-c2dm


    【解决方案1】:

    您可以在 GCMIntentService 意图类中的 onMessage 侦听器上的 SharedPreferences 中保存一些数据。 GCM 监听器毕竟属于你的包应用程序。 您保存的内容取决于您的应用程序和消息负载,但它可能是您想要的任何内容。 然后在单击通知时启动的 Activity 的 onCreate 函数上,您阅读 Shared Preferences 以查看您是否来自 GCM 通知。请记住清除您保存在 SharedPreferences 中的变量,以便下次用户打开应用程序时,它可以正确显示内容。

    这里有一个例子。不幸的是,我现在无法尝试,但看到这个想法很有用。它与 G2DM 非常相似,因此您必须在您的情况下寻找等价物。

    public class GCMIntentService extends GCMBaseIntentService {
    
        /*... other functions of the class */
    
        /**
         * Method called on Receiving a new message
         * */
        @Override
        protected void onMessage(Context context, Intent intent) {
            Log.i(TAG, "Received message");
            String message = intent.getExtras().getString("your_message");
    
            // notifies user
            generateNotification(context, message);
        }
    
        /**
         * Issues a notification to inform the user that server has sent a message.
         */
        private static void generateNotification(Context context, String message) {
            int icon = R.drawable.ic_launcher;
            long when = System.currentTimeMillis();
            NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notification = new Notification(icon, message, when);
    
            // Save your data in the shared preferences
            SharedPreferences prefs = getSharedPreferences("YourPrefs", MODE_PRIVATE);  
            SharedPreferences.Editor prefEditor = prefs.edit();  
            prefEditor.putBoolean("comesFromGCMNotification", true);  
            prefEditor.commit(); 
    
            String title = context.getString(R.string.app_name);
    
            Intent notificationIntent = new Intent(context, MainActivity.class);
            // set intent so it does not start a new activity
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent intent =
                PendingIntent.getActivity(context, 0, notificationIntent, 0);
            notification.setLatestEventInfo(context, title, message, intent);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
    
            // Play default notification sound
            notification.defaults |= Notification.DEFAULT_SOUND;
    
            // Vibrate if vibrate is enabled
            notification.defaults |= Notification.DEFAULT_VIBRATE;
            notificationManager.notify(0, notification);     
    
        }
    
    }
    

    【讨论】:

    • 感谢您的回复。 :-) 这与我已经在做的事情非常相似,尽管显然没有使用我们正在谈论的 sharedprefs 。我难以理解的是“comesFromGCMNotification”部分以及我如何准确判断用户是否从通知中启动。在我看来,在用户收到通知并设置“comesFromGCMNotification”后,如果他们在没有选择通知的情况下启动应用程序,那么“comesFromGCMNotification”仍然是正确的,并且仍然会进行任何处理。我的推理错了吗?
    • @MrTristan 检查您是否在两个地方(Activity 和 GCMIntentService)使用相同的首选项。在我的示例中,它们被称为“YourPrefs”。当您要求不存在的首选项时,您还可以检查默认返回值。要对此进行测试,请对“comesFromGCMNotification”使用字符串而不是布尔值,并检查结果是否真的是您输入的而不是默认值(可能是空字符串或 null)。
    • 我认为我的评论措辞不正确。读取 comeFromGCMNotification 的值不是问题……我认为在概念层面上,流程是错误的。我认为,在您收到通知并在收到通知时设置了该偏好后,当您在不选择通知的情况下启动应用程序时,它仍然是正确的......我希望能够根据您是否启动有不同的体验通过选择通知应用程序或通过主屏幕启动应用程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-22
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多