【问题标题】:Android - Pass data when user press notificationAndroid - 用户按下通知时传递数据
【发布时间】:2017-03-13 11:15:53
【问题描述】:

我有以下代码,我想在用户点击通知时检索一些数据。但我不知道如何处理它。 我知道有一种方法可以将活动作为参数传递,但取决于通知的内容,我应该打开不同的活动。

这是愿望工作流程

我发布通知(带有键值数据)-> 用户点击通知-> 广播接收器(检索键值数据)-> 活动

问题是我的 BroadcastReceiver 没有接收数据。

    Intent notificationIntent = new Intent(NOTIFICATION_SELECTED_ACTION);
    notificationIntent.putExtra("mykey","myvalue");
    PendingIntent intent = PendingIntent.getBroadcast(context, 0, notificationIntent, 0);
    context.getApplicationContext().registerReceiver(receiverOpen, new IntentFilter(NOTIFICATION_SELECTED_ACTION));

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
    ...
    mBuilder.setContentIntent(intent);
    //I publish the notification in the notification bar
    notification = mBuilder.build();
    mNotificationManager.notify(id_push, notification);


   private final BroadcastReceiver receiverOpen = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        Intent i = new Intent(context.getApplicationContext(),MainActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        //Value is always null ffs
        String value = intent.getStringExtra("mykey");

        count = 0; // Do what you want here
        notificationList.clear();
        editor.clear();
        editor.commit();

        context.startActivity(i);
    }
};

【问题讨论】:

  • 当您按下通知时。如果您有来自通知的价值,那么应该没有任何问题。在mainActivity中根据值可以开启不同的不同activity。

标签: android android-notifications


【解决方案1】:

在您的代码中,您正在使用广播服务。如果您在应用程序处于前台时收到推送通知。您可以使用如下广播服务

            Intent intent = new Intent("myPush");
            intent.putExtra("message", messageBody);
            intent.putExtra("title", title);
            intent.putExtra("Id", id);
            getApplicationContext().sendBroadcast(intent);

您可以在应用中的任何位置注册广播公司。如下所示

@Override
    public void onResume() {
        super.onResume();
        getActivity().registerReceiver(mMessageReceiver, new IntentFilter("myPush"));
    }

所以当推送到达时,您可以使用以下方法启动任何方法以转到首选活动。

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            // Extract data included in the Intent
            String message = intent.getStringExtra("message");
            String title = intent.getStringExtra("title");
            final String orderId = intent.getStringExtra("Id");

            Log.e("Main Activity",title+" : "+message);

           //AnyIntent or Query

        }
    };

当应用程序在后台时。

            Intent intent = new Intent(this, MainActivity.class);
            intent.putExtra("message", messageBody);
            intent.putExtra("title", title);
            intent.putExtra("Id", Id);

            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                    PendingIntent.FLAG_ONE_SHOT);

并在主 Activity 的 onCreate 方法中处理它。

            Intent intent = getIntent();
            String receivedId = intent.getStringExtra("Id");

           if(receivedId != null){
             //Do Somthing as you want
           }

【讨论】:

  • 正如我所说,我想在用户点击通知时处理应用程序。
  • @Ricardo 我已经更新了这个问题。当您按下通知时,您必须使用待处理的意图。它会接收到mainActivity,您可以根据需要进行处理
  • 不,因为当用户点击通知并触发 BroadcastReceiver 时数据未到达。你应该检查我的代码。并阅读我的评论:“价值始终为空 ffs”。当用户点击通知时,您认为我有数据,这是错误的。
  • 如果你得到空值。那么键值对应该有问题。可能是你读错了键名
【解决方案2】:

根据这篇帖子https://stackoverflow.com/a/6752571/2139691 我应该使用 PendingIntent.FLAG_CANCEL_CURRENT

PendingIntent intent = PendingIntent.getBroadcast(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

【讨论】:

    【解决方案3】:

    首先,您需要在通知中具有价值。 通过 Intent 将数据从 Notification 活动传递到您的 Intent 活动。 参见例如。

     Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("point", "2");
        intent.putExtra("link", link);
    

    将此从通知传递到 MainActivity, 在 MainActivity 中,

       if (getIntent().hasExtra("point")) {
            String link = getIntent().getStringExtra("link)
    

    使用这个“链接”我打开了网站。 谢谢你..!!

    【讨论】:

    • 这不起作用,因为如果你阅读我的代码你可以看到我的评论“//Value is always null ffs”
    • 然后代替'Intent notificationIntent = new Intent(NOTIFICATION_SELECTED_ACTION);'这个尝试使用'Intent i=new Intent(this,your_activity_name.class);'
    • 不,我说我想在用户点击通知时处理应用程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多