【问题标题】:android notification click not workingandroid通知点击不起作用
【发布时间】:2014-05-12 04:54:49
【问题描述】:

我在服务中使用此代码,以便在我有任何新警报时收到通知,但是当我点击它们时,我没有进入我想要的视图:

if(newAlertCounter > 0) // alert user about new alerts
{
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.ic_action_warning)
    .setContentTitle(newAlertCounter + " new flood alerts!")
    .setContentText("Tap to see where in your vecinity.");

    // Sets an ID for the notification
    int mNotificationId = 001;
    // Gets an instance of the NotificationManager service
    NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // Builds the notification and issues it.
    mNotifyMgr.notify(mNotificationId, mBuilder.build());

    // notification click action
    Intent notificationIntent = new Intent(this, ViewAlertsActivity.class);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder.setContentIntent(resultPendingIntent);
}

显示出来了,但是不能点击,这是怎么回事?

【问题讨论】:

    标签: android android-notifications


    【解决方案1】:

    mNotifyMgr.notify(mNotificationId, mBuilder.build()); 
    

    之后

    mBuilder.setContentIntent(resultPendingIntent);
    

    【讨论】:

      【解决方案2】:

      移动你的

      mNotifyMgr.notify(mNotificationId, mBuilder.build());
      

      之后

      mBuilder.setContentIntent(resultPendingIntent);
      

      当您调用.build() 时,您会创建不带content intent 的通知。 (不,它不会在之后添加,因为将发送到通知系统的对象将是Notification 而不是Builder


      if(newAlertCounter > 0) // alert user about new alerts
      {
          NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
          .setSmallIcon(R.drawable.ic_action_warning)
          .setContentTitle(newAlertCounter + " new flood alerts!")
          .setContentText("Tap to see where in your vecinity.");
      
          // Sets an ID for the notification
          int mNotificationId = 001;
          // Gets an instance of the NotificationManager service
          NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
          // Builds the notification and issues it.
      
      
          // notification click action
          Intent notificationIntent = new Intent(this, ViewAlertsActivity.class);
      
          PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
      
          mBuilder.setContentIntent(resultPendingIntent);
      
          mNotifyMgr.notify(mNotificationId, mBuilder.build());
      }
      

      【讨论】:

        猜你喜欢
        • 2022-12-10
        • 1970-01-01
        • 1970-01-01
        • 2023-03-03
        • 1970-01-01
        • 2020-07-26
        • 2017-04-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多