• <legend id='OEXya'><style id='OEXya'><dir id='OEXya'><q id='OEXya'></q></dir></style></legend>
      <bdo id='OEXya'></bdo><ul id='OEXya'></ul>
      <tfoot id='OEXya'></tfoot>

      <i id='OEXya'><tr id='OEXya'><dt id='OEXya'><q id='OEXya'><span id='OEXya'><b id='OEXya'><form id='OEXya'><ins id='OEXya'></ins><ul id='OEXya'></ul><sub id='OEXya'></sub></form><legend id='OEXya'></legend><bdo id='OEXya'><pre id='OEXya'><center id='OEXya'></center></pre></bdo></b><th id='OEXya'></th></span></q></dt></tr></i><div id='OEXya'><tfoot id='OEXya'></tfoot><dl id='OEXya'><fieldset id='OEXya'></fieldset></dl></div>

      <small id='OEXya'></small><noframes id='OEXya'>

      1. 应用程序在后台时的 Android 通知

        时间:2024-08-24
        <legend id='SVFbt'><style id='SVFbt'><dir id='SVFbt'><q id='SVFbt'></q></dir></style></legend>
        • <bdo id='SVFbt'></bdo><ul id='SVFbt'></ul>

            <small id='SVFbt'></small><noframes id='SVFbt'>

              <tbody id='SVFbt'></tbody>

            <tfoot id='SVFbt'></tfoot>
          1. <i id='SVFbt'><tr id='SVFbt'><dt id='SVFbt'><q id='SVFbt'><span id='SVFbt'><b id='SVFbt'><form id='SVFbt'><ins id='SVFbt'></ins><ul id='SVFbt'></ul><sub id='SVFbt'></sub></form><legend id='SVFbt'></legend><bdo id='SVFbt'><pre id='SVFbt'><center id='SVFbt'></center></pre></bdo></b><th id='SVFbt'></th></span></q></dt></tr></i><div id='SVFbt'><tfoot id='SVFbt'></tfoot><dl id='SVFbt'><fieldset id='SVFbt'></fieldset></dl></div>

                  本文介绍了应用程序在后台时的 Android 通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在从 google firebase 为我的 Android 应用程序发送推送通知,目标为 Android 5.0:

                  I am sending push notification from google firebase for my android app with target of Android 5.0:

                  我的推送通知代码是:

                  @Override
                  public void onMessageReceived(RemoteMessage remoteMessage) {
                      String badge = "0";
                      Uri uri = Uri.parse(
                              getString(R.string.app_host_name)
                      );
                  
                      Map<String, String> data = remoteMessage.getData();
                      if (data.size() > 0) {
                          try {
                              uri = Uri.parse(
                                      data.get("link")
                              );
                  
                              badge = data.get("badge");
                          } catch (NullPointerException e) {
                              //
                          }
                      }
                  
                      if (remoteMessage.getNotification() != null) {
                          RemoteMessage.Notification notification = remoteMessage.getNotification();
                          sendNotification(notification.getTitle(), notification.getBody(), uri.toString(), badge);
                      }
                  }
                  
                  
                  
                  private void sendNotification(String title, String body, String url, String badge) {
                      Intent intent = new Intent(this, MainActivity.class);
                      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                  
                      if (Patterns.WEB_URL.matcher(url).matches()) {
                          intent.putExtra("link", url);
                      }
                  
                      PendingIntent pendingIntent = PendingIntent.getActivity(
                              this,
                              0,
                              intent,
                              PendingIntent.FLAG_UPDATE_CURRENT
                      );
                  
                      Resources resources = getApplicationContext().getResources();
                  
                      NotificationCompat.Builder notificationBuilder =
                              new NotificationCompat.Builder(this, "default")
                                      .setColor(
                                              resources.getColor(R.color.colorPrimaryDark)
                                      )
                                      .setSmallIcon(
                                              R.drawable.ic_stat_icon
                                      )
                                      .setContentTitle(title)
                                      .setContentText(body)
                                      .setAutoCancel(true)
                                      .setNumber(Integer.parseInt(badge))
                                      .setLargeIcon(
                                              BitmapFactory.decodeResource(
                                                      resources,
                                                      R.mipmap.ic_launcher
                                              )
                                      )
                                      .setContentIntent(pendingIntent);
                  
                      NotificationManager notificationManager =
                              (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                  
                      if (Build.VERSION.SDK_INT >= 26) {
                          NotificationChannel notificationChannel = new NotificationChannel(
                                  "default",
                                  "Main notification channel",
                                  NotificationManager.IMPORTANCE_HIGH
                          );
                  
                          notificationManager.createNotificationChannel(
                                  notificationChannel
                          );
                      }
                  
                      notificationManager.notify(
                              1,
                              notificationBuilder.build()
                      );
                  }
                  

                  当应用程序处于活动/打开/不在后台时,一切都非常完美,但是当它不是时,通知不会分组,没有显示数字,并且对所有这些设置都没有反应,我是什么只能通过清单设置更改小图标和圆圈颜色

                  And everything is super perfect when application is active/opened/not in background, but when it is not, notifications are not grouped, there is no number displayed, and no reaction on all of this settings at all, what i was able to change is only small icon and circle color via manifest settings

                      <meta-data
                          android:name="com.google.firebase.messaging.default_notification_icon"
                          android:resource="@drawable/ic_stat_icon" />
                      <meta-data
                          android:name="com.google.firebase.messaging.default_notification_color"
                          android:resource="@color/colorPrimaryDark" />
                  

                  但是为什么呢?就像当应用程序处于后台时,通知没有使用 Activity 代码中的设置,而是仅使用 AndroidManifest 中的某种默认"设置.

                  but why? It's like when app is in background notifications are not using settings from Activity code but using only some kind of "default" one from AndroidManifest.

                  推荐答案

                  正如你在评论中所说:

                  当应用处于后台时,应用不采用 setNumber、setAutoCancel、setSmallIcon、setLargeIcon 选项

                  这是因为您使用通知负载发送仅在前台触发的通知.

                  It is because you are using notification payload to send the notification which only triggers on the foreground.

                  所以当你的应用在后台时它不会进入这个方法.

                  So when your app is in the background it does not enter this method.

                  要解决这个问题,您可以单独使用 data 负载:

                  to solve this you can use data payload alone:

                  "data": {
                  "titles": "New Title",
                  "bodys": "body here"
                  }
                  

                  因为数据负载将进入 onMessageReceived() 当你的应用在前台/后台时.

                  since the data payload will enter the onMessageReceived() when your app is in foreground/background.

                  然后在 fcm 中你可以这样做:

                  then in fcm you can do this:

                    if (remoteMessage.getData().size() > 0) {
                  
                          title = remoteMessage.getData().get("titles");
                          body = remoteMessage.getData().get("bodys");
                      }
                  

                  这篇关于应用程序在后台时的 Android 通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                    <tbody id='jVVR4'></tbody>
                  <i id='jVVR4'><tr id='jVVR4'><dt id='jVVR4'><q id='jVVR4'><span id='jVVR4'><b id='jVVR4'><form id='jVVR4'><ins id='jVVR4'></ins><ul id='jVVR4'></ul><sub id='jVVR4'></sub></form><legend id='jVVR4'></legend><bdo id='jVVR4'><pre id='jVVR4'><center id='jVVR4'></center></pre></bdo></b><th id='jVVR4'></th></span></q></dt></tr></i><div id='jVVR4'><tfoot id='jVVR4'></tfoot><dl id='jVVR4'><fieldset id='jVVR4'></fieldset></dl></div>
                1. <legend id='jVVR4'><style id='jVVR4'><dir id='jVVR4'><q id='jVVR4'></q></dir></style></legend>

                  <small id='jVVR4'></small><noframes id='jVVR4'>

                  • <tfoot id='jVVR4'></tfoot>
                      <bdo id='jVVR4'></bdo><ul id='jVVR4'></ul>