【问题标题】:Android notification(when the app is open)Android 通知(当应用程序打开时)
【发布时间】:2014-10-29 12:29:39
【问题描述】:

我在 android 服务中使用此代码发送通知:

notification.setLatestEventInfo(this, "title", "text", pendingIntent); 
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(id, notification);

如果应用程序关闭,此代码可以正常工作,但我也希望它在应用程序(应用程序的任何活动)打开时也发送通知,当应用程序打开时,我没有收到通知。所以请告诉我即使应用程序打开了如何接收通知。

【问题讨论】:

    标签: android notifications android-service android-notifications


    【解决方案1】:

    public static final int FLAG_ONGOING_EVENT

    自:API 级别 1 如果此通知涉及正在进行的事情(例如电话),则将按位或位到标志字段中应设置的位。

    public static final int FLAG_FOREGROUND_SERVICE

    自:API 级别 5 位将按位或按位运算到标志字段中,如果此通知表示当前正在运行的服务,则应设置该字段。

    尝试使用:

    notification.flags |= Notification.FLAG_FOREGROUND_SERVICE | Notification.FLAG_NO_CLEAR
    

    【讨论】:

      【解决方案2】:

      不适合我... 与

      notification.flags |= Notification.FLAG_FOREGROUND_SERVICE | Notification.FLAG_NO_CLEAR
      

      点击后通知保持不变。

      我已按照本教程http://programmerguru.com/android-tutorial/how-to-send-push-notifications-using-gcm-service/ 进行操作,一切正常,但是当打开应用程序并点击收到的通知时,什么也没有出现....

      private void sendNotification(String trmMsg,String user, int role) {
          Intent resultIntent = new Intent(this, MainActivity.class);
          resultIntent.putExtra("trmjson", trmMsg);
          resultIntent.putExtra("fragment", "readN");
          resultIntent.setAction(Intent.ACTION_MAIN);
          resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
          PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,resultIntent, PendingIntent.FLAG_ONE_SHOT);
      
          NotificationCompat.Builder mNotifyBuilder;
          NotificationManager mNotificationManager;
      
          mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      
          String msg = role == 1 ? user + " conferma il cambio di turno!" : "Attenzione, cambio di turno!";
      
          mNotifyBuilder = new NotificationCompat.Builder(this)
                  .setContentTitle("TRM")
                  .setContentText(msg)
                  .setSmallIcon(R.drawable.ic_notific);
          mNotifyBuilder.setContentIntent(resultPendingIntent);
      
          int defaults = 0;
          defaults = defaults | Notification.DEFAULT_LIGHTS;
          defaults = defaults | Notification.DEFAULT_VIBRATE;
          defaults = defaults | Notification.DEFAULT_SOUND;
      
          mNotifyBuilder.setDefaults(defaults);
          mNotifyBuilder.setContentText(msg);
          mNotifyBuilder.setAutoCancel(true);
          mNotificationManager.notify(notifyID, mNotifyBuilder.build());
      }
      

      【讨论】:

      • 我已经在 mNotificationManager.notify(notifyID, mNotifyBuilder.build()); 之后使用 System.exit(0) 解决了但我知道这不是正确的解决方案...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 2018-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多