应用程序在后台时的 vpn下载 Android 通知

2024-08-24Java开发问题
0

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

问题描述

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

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

我的推送通知代码是:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
   free vpn  String badge = "0";
    Uri uri = Uri.parse(
            getString(R.string.app_host_name)
    免费vpn );

    Map<String, String> data = remoteMessage.getData();
   vpn下载  if (data.size() > 0) {
 免费vpn      免费vpn   try {
            免费vpn下载 uri = Uri.parse(
     免费vpn                data.get("link")
       vpn下载      );

       vpn下载   免费vpn    badge = data.get("badge");
        } 免费vpn catch (NullPointerException e) {
 免费vpn下载            //
        }
    }

    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);
 免费vpn    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    if (Patterns.WEB_URL.matcher(url).matches()) {
        intent.putExtra("link", url);
    }

    vpn下载 PendingIntent pendingIntent = PendingIntent.getActivity(
   free vpn          vpn下载 this,
     免费vpn下载        0,
            intent,
    vpn下载     vpn free     免费vpn下载 PendingIntent.FLAG_UPDATE_CURRENT
  免费vpn下载   );

    Resources resources = getApplicationContext().getResources();

    NotificationCompat.Builder notificationBuilder =
      免费vpn下载     免费vpn下载 free vpn   new NotificationCompat.Builder(this, "default")
     免费vpn下载      免费vpn           .setColor(
                            resources.getColor(R.color.colorPrimaryDark)
             vpn free      免费vpn   )
     free vpn                .setSmallIcon(
                            R.drawable.ic_stat_icon
  vpn free                   )
         vpn下载  免费vpn下载           .setContentTitle(title)
                    .setContentText(body)
                    .setAutoCancel(true)
                    .setNumber(Integer.parseInt(badge))
                    .setLargeIcon(
        vpn下载                免费vpn      BitmapFactory.decodeResource(
            vpn下载         vpn下载                 resources,
                      vpn下载  vpn下载   免费vpn     vpn free  免费vpn     vpn下载 vpn free   R.mipmap.ic_launcher
                     免费vpn        )
                    )
        免费vpn下载             .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
    free vpn        free vpn  (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= 26) {
  免费vpn       NotificationChannel 免费vpn notificationChannel = new NotificationChannel(
 免费vpn          免费vpn   free vpn     "default",
        vpn下载      free vpn    "Main notification channel",
 vpn free   vpn下载   vpn free            NotificationManager.IMPORTANCE_HIGH
        );

        notificationManager.createNotificationChannel(
      vpn下载           notificationChannel
     免费vpn    );
    }

    notificationManager.notify(
          vpn free   1,
 免费vpn free vpn    免费vpn         notificationBuilder.build()
    vpn下载 );
}

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

And everything is super perfect 免费vpn vpn下载 when application is active/opened/not in free vpn 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 free vpn change is only small icon and circle color 免费vpn下载 via manifest settings

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

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

but vpn free why? It's like when app is in background notifications are not using settings from Activity code free vpn 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 免费vpn下载 foreground.

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

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

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

to solve 免费vpn 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.

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

then in fcm you can vpn下载 do this:

  免费vpn if (remoteMessage.getData().size() vpn下载 > 0) {

        vpn free title = remoteMessage.getData().get("titles");
   vpn下载      body = remoteMessage.getData().get("bodys");
 vpn free    }

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

The free vpn End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
vpn下载 2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How free vpn to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件 免费vpn下载
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 vpn下载 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to 免费vpn下载 make vpn free them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13