Java 应用程序处于后台时的Android通知

Java 应用程序处于后台时的Android通知,java,android,firebase,firebase-cloud-messaging,Java,Android,Firebase,Firebase Cloud Messaging,我正在从google firebase为我的android应用程序发送推送通知,目标为android 5.0: 我的推送通知代码是: @Override public void onMessageReceived(RemoteMessage remoteMessage) { String badge = "0"; Uri uri = Uri.parse( getString(R.string.app_host_name) ); Map&l

我正在从google firebase为我的android应用程序发送推送通知,目标为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()
    );
}
@覆盖
收到消息时公共无效(RemoteMessage RemoteMessage){
字符串badge=“0”;
Uri=Uri.parse(
getString(R.string.app\u主机名)
);
Map data=remoteMessage.getData();
如果(data.size()>0){
试一试{
uri=uri.parse(
data.get(“链接”)
);
badge=data.get(“badge”);
}捕获(NullPointerException e){
//
}
}
if(remoteMessage.getNotification()!=null){
RemoteMessage.Notification Notification=RemoteMessage.getNotification();
sendNotification(notification.getTitle()、notification.getBody()、uri.toString()、badge);
}
}
私有void sendNotification(字符串标题、字符串正文、字符串url、字符串标记){
意向意向=新意向(此,MainActivity.class);
intent.addFlags(intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
if(Patterns.WEB\u URL.matcher(URL.matches()){
intent.putExtra(“链接”,url);
}
PendingEvent PendingEvent=PendingEvent.getActivity(
这
0,
意图
PendingEvent.FLAG_更新_当前
);
Resources=getApplicationContext().getResources();
NotificationCompat.Builder notificationBuilder=
新建NotificationCompat.Builder(此为“默认值”)
.setColor(
resources.getColor(R.color.colorPrimaryDark)
)
.setSmallIcon(
R.drawable.ic\u stat\u图标
)
.setContentTitle(标题)
.setContentText(正文)
.setAutoCancel(真)
.setNumber(整数.parseInt(徽章))
setLargeIcon先生(
BitmapFactory.decodeResource(
资源,
R.mipmap.ic_启动器
)
)
.setContentIntent(挂起内容);
通知经理通知经理=
(NotificationManager)getSystemService(上下文通知服务);
如果(Build.VERSION.SDK_INT>=26){
NotificationChannel NotificationChannel=新建NotificationChannel(
“默认”,
“主通知通道”,
通知经理。重要性高
);
notificationManager.createNotificationChannel(
通知频道
);
}
notificationManager.notify(
1.
notificationBuilder.build()
);
}
当应用程序处于活动状态/打开状态/不在后台时,一切都非常完美,但当应用程序处于活动状态/打开状态/不在后台时,通知不会分组,不会显示任何数字,对所有这些设置都没有反应,我所能更改的只是通过清单设置的小图标和圆圈颜色

    <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" />


但是为什么呢?就像应用程序处于后台时,通知不使用活动代码中的设置,而只使用AndroidManifest中的某种“默认”设置一样。

正如您在评论中所说:

当应用程序处于后台时,应用程序不接受setNumber、setAutoCancel、SETMALLICON、SETLARGECON选项

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

因此,当你的应用程序在后台时,它不会进入此方法

要解决此问题,您可以单独使用
数据
有效负载:

"data": {
"titles": "New Title",
"bodys": "body here"
}
因为当你的应用程序在前台/后台时,数据负载将进入

然后在fcm中,您可以执行以下操作:

  if (remoteMessage.getData().size() > 0) {

        title = remoteMessage.getData().get("titles");
        body = remoteMessage.getData().get("bodys");
    }

你可以使用透明图标来通知。是的,我知道,但问题不是图标,问题是当应用程序在后台时,所有选项都不同,应用程序没有使用setNumber、setAutoCancel、SetMallIcon、setLargeIcon选项,没有任何未在我的发帖中定义的内容,我已经这样做了,但这不起作用,它对通知图标没有影响例如标题和正文似乎是通知有效载荷,这是
notification.getTitle(),notification.getBody()
结果是我发送的通知消息带有数据有效载荷,而不是数据消息()@MyMomsays特别请将此标记为已解决。如果提供的解决方案已解决问题。。或者,请使用您找到的解决方案(如果有)进行更新。@PeterHaddad nope.:(