【问题标题】:Android Pushnotification, does nothing on click when inside appAndroid推送通知,在应用程序内点击时不执行任何操作
【发布时间】:2016-04-27 13:48:28
【问题描述】:

当点击推送通知时,我正在尝试启动一项活动。 有趣的是,当应用程序关闭且后台没有活动时,我收到推送,我单击它并打开所需的活动。 但是当应用程序的任何活动被打开并且我收到推送时,点击它什么都不做。 我尝试过差异组合。还要将exported = true 放在启动活动中。

这是我的代码:

private static void showNotification(Context context,String title,String text,Intent openIntent)
{


    PendingIntent pi = PendingIntent.getActivity(context, 0, openIntent, PendingIntent.FLAG_CANCEL_CURRENT|PendingIntent.FLAG_ONE_SHOT);

   // Resources r = getResources();
   final Notification notification = new NotificationCompat.Builder(context)
            .setTicker(title)

            .setSmallIcon(R.drawable.notif_icon)
           .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
                   R.drawable.notif_icon))
            .setContentTitle(title)
            .setContentText(text)
            .setContentIntent(pi)
            .setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS|Notification.DEFAULT_VIBRATE)
            .setAutoCancel(false)
            .build();

    // also tried .setAutoCancel(true) above instead of cancel
    notification.flags |= Notification.FLAG_AUTO_CANCEL;


   new Handler().postDelayed(new Runnable() {
       @Override
       public void run() {
           notificationManager.notify(0, notification);
       }
   },1000);


}

现在,我应该尝试什么组合?删除标志 CANCEL_CURRENT 或 FLAG_ONE_SHOT ?已经试过了。即使没有这两个标志,点击通知也没有结果。

我在 android 5.1 / 5.0 上运行该应用程序。我已经读到这些 android 版本的通知存在问题。所以我已经尝试过像 android:exported=true 和 FLAG_CANCEL current 这样的解决方案。但没有效果。

现在如何使通知点击工作并打开预期的活动?

感谢任何帮助。 谢谢

【问题讨论】:

    标签: android notifications push-notification android-5.0-lollipop android-5.1.1-lollipop


    【解决方案1】:

    像这样使用 FLAG_UPDATE_CURRENT。它为我工作

    PendingIntent.getActivity(context, 0, openIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    

    【讨论】:

      【解决方案2】:

      您需要从 FirebaseMessageingService 类广播数据

      public void onMessageReceived(RemoteMessage remoteMessage)
      

      并在您想显示数据的任何地方接收广播。

      【讨论】:

        【解决方案3】:
        /*I tried this code on asus zenfone 2 (android 5.0), it is working without any problem please try this.*/
         private void pushNotificationWithClickAction(String mainTittle,String subTittle,Intent intent){
               PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
               Notification noti = new NotificationCompat.Builder(this)
                        .setContentTitle(mainTittle)
                        .setContentText(subTittle)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentIntent(pIntent).build();
                noti.flags |= Notification.FLAG_AUTO_CANCEL;
                NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                notificationManager.notify(0, noti);
            }
        

        【讨论】:

        • 好的,我试一试,让你知道它是否有效。
        • 我试过你的代码,但结果相同。它工作一两次,然后就不起作用了。我的代码也是如此。它打开活动一次或两次,然后我进出应用程序。再次转到主页,它停止工作。点击不起作用。但是今天我注意到这一行显示在调试中,当我单击推送时没有任何反应: startActivity 从非活动上下文调用;强制 Intent.FLAG_ACTIVITY_NEW_TASK 用于:Intent { cmp=com.mypackage/.Activities.ChatDetailActivity
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多