当App不在前台或未运行时,Remoteview显示系统通知,而不是“自定义通知" [英] Remoteview displays system notification when App is not in the foreground or not running instead of Custom Notification

查看:87
本文介绍了当App不在前台或未运行时,Remoteview显示系统通知,而不是“自定义通知"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,让我告诉您到目前为止我做了什么. 我经历了很多搜索,包括

First of all, let me tell you what I have done so far. I have gone through lots of searching including

Remoteview不在后台应用

firebase_messaging在应用程序不在前台时未自定义通知

firebase_messaging not customizing notification when an app is not in the foreground

此外,我已经引用了GitHub和许多文章,但是这些文章都不适合我,所以这就是我发布此问题的原因,所以请不要将其标记为重复.

当应用程序运行时,它可以正常工作,但是当应用程序在后台时,它会显示系统通知.

Its Working fine when App is Running, but it displays system notification when the app is in background.

我们是否需要添加custom_notification_layout引用以显示清单?(是否有meta tag或其他内容?)

Do we need to add custom_notification_layout reference to manifest? (is there any meta tag or anything?)

这是MyFireBaseMessagingService.java

This is MyFireBaseMessagingService.java class

public class MyFireBaseMessagingServiceTmp extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        JSONObject jsonObject = new JSONObject(remoteMessage.getData());
        String title = jsonObject.optString("title");
        String message = jsonObject.optString("message");

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= 26) {
            NotificationChannel channel = new NotificationChannel("my_notifications", "My notifications", NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }
        RemoteViews remoteViews;
        if (title == "BIG BANG") {
            remoteViews = new RemoteViews(getPackageName(), R.layout.layout_remainder_with_buttons);
        } else {
            remoteViews = new RemoteViews(getPackageName(), R.layout.layout_remainder_no_buttons);
        }
        remoteViews.setTextViewText(R.id.txtTitle, title);
        remoteViews.setTextViewText(R.id.txtMsg, message);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "my_notifications");
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setCustomBigContentView(remoteViews);
        {
            Intent intentGotIt = new Intent("ACTION_GOT_IT");
            PendingIntent pendingIntentGotIt = PendingIntent.getBroadcast(this, 0, intentGotIt, PendingIntent.FLAG_ONE_SHOT);
            remoteViews.setOnClickPendingIntent(R.id.btnGotIt, pendingIntentGotIt);
        }
        {
            Intent intentSnooze = new Intent("ACTION_SNOOZE");
            PendingIntent pendingIntentSnooze = PendingIntent.getBroadcast(this, 0, intentSnooze, PendingIntent.FLAG_ONE_SHOT);
            remoteViews.setOnClickPendingIntent(R.id.btnSnooze, pendingIntentSnooze);
        }
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
        notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
        notificationBuilder.setAutoCancel(true);
        Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        notificationBuilder.setLargeIcon(icon);
        Notification notification = notificationBuilder.build();
        notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        notification.vibrate = new long[]{0, 400, 200, 400};
        notificationManager.notify(1000, notification);
    }
}

这是layout_remainder_with_buttons.xml 布局

This is layout_remainder_with_buttons.xml layout

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="110dp"
        android:background="@drawable/bg_gradient_blue">

        <ImageView
            android:id="@+id/ivLogo"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_centerVertical="true"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="5dp"
            android:layout_marginBottom="10dp"
            android:src="@drawable/logo_round" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toEndOf="@+id/ivLogo"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:padding="5dp">

        <TextView
            android:id="@+id/txtTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:textStyle="bold"
            tools:text="Notification title" />

        <TextView
            android:id="@+id/txtMsg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/txtTitle"
            android:textColor="@color/white"
            android:textSize="14sp"
            tools:text="Notification msg" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            tools:visibility="visible">

            <Button
                android:id="@+id/btnGotIt"
                android:layout_width="100dp"
                android:layout_height="30dp"
                android:layout_marginEnd="10dp"
                android:background="@drawable/bg_button_oval_green"
                android:gravity="center"
                android:padding="5dp"
                android:text="Got it...!"
                android:textAllCaps="true"
                android:textColor="@color/white"
                android:textStyle="bold" />

            <Button
                android:id="@+id/btnSnooze"
                android:layout_width="100dp"
                android:layout_height="30dp"
                android:layout_marginStart="10dp"
                android:layout_marginEnd="10dp"
                android:background="@drawable/bg_button_oval_red"
                android:gravity="center"
                android:padding="5dp"
                android:text="Snooze"
                android:textAllCaps="true"
                android:textColor="@color/white"
                android:textStyle="bold" />
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

这是layout_remainder_no_buttons.xml

This is layout_remainder_no_buttons.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:background="@drawable/bg_gradient_blue">

    <ImageView
        android:id="@+id/ivLogo"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_centerVertical="true"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="5dp"
        android:src="@drawable/logo_round" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toEndOf="@+id/ivLogo"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:padding="5dp">

        <TextView
            android:id="@+id/txtTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Remainder"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/txtMsg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/txtTitle"
            android:ellipsize="end"
            android:maxLines="2"
            android:text="Its time to place order for `For Father`"
            android:textColor="@color/white"
            android:textSize="14sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/txtMsg"
            android:gravity="center_vertical|center_horizontal"
            android:orientation="horizontal"
            android:visibility="gone">

            <Button
                android:id="@+id/btnSave"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginStart="10dp"
                android:layout_marginEnd="10dp"
                android:layout_weight="1"
                android:background="@null"
                android:gravity="center"
                android:padding="5dp"
                android:text="Got it...!"
                android:textAllCaps="true"
                android:textColor="@color/white"
                android:textStyle="bold" />

            <Button
                android:id="@+id/btnCancel"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginStart="10dp"
                android:layout_marginEnd="10dp"
                android:layout_weight="1"
                android:background="@null"
                android:gravity="center"
                android:padding="5dp"
                android:text="Snooze"
                android:textAllCaps="true"
                android:textColor="@color/app_red"
                android:textStyle="bold" />
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

推荐答案

从ServerSide

From ServerSide

请确保在"notification payload" 您正在NOT发送" notification"字段,如果要发送? ,然后请从您的服务器请求中删除notification字段,并仅发送数据.

Please make sure that "You are NOT sending "notification" field in the "notification payload" and if you are sending? then please Remove notification field from your server request and send the only data.

因为您的应用在后台运行时,Android将NOT执行onMessageReceived,系统将接管该通知并显示正常通知".

because when your app is in the background than Android will NOT execute onMessageReceived and system will take over that notification and will show "Normal Notification".

因此,在通知有效负载中仅发送"data". 还要在通知有效负载中添加{"priority": "high"}.

So, send only "data" in your notification payload. Also add {"priority": "high"} in your notification payload.

以下是服务器响应的示例

Here is the example for server response

{
  "to": "your_fcm_token",
  "priority": "high",
  "data":{
    "title": "your_title",
    "msg": "your_message",
    "other_info": "extra_info"
  }
}

希望这会有所帮助:)

这篇关于当App不在前台或未运行时,Remoteview显示系统通知,而不是“自定义通知"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆