【问题标题】:Why does my app always show the default icon on a push notification display in system tray?为什么我的应用程序总是在系统托盘的推送通知显示中显示默认图标?
【发布时间】:2019-08-27 20:03:12
【问题描述】:

这个question and related ones 已经被问过很多次了,但是我正在寻求帮助以解决我的具体问题,因为我已经查看了Firebase guides 和各种questions/answers 关于如何在推送通知中添加图标并且可以似乎没有得到它。

我相信我在某处犯了一个小错误。我希望你能看一眼我的代码并发现它。

这是我应用的 AndroidManifest.xml。

<application
    android:allowBackup="false"
    android:hardwareAccelerated="true"
    android:icon="@mipmap/ic_launcher"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:label="@string/app_name"
    android:theme="@style/AppThemeFull">
    <meta-data android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@mipmap/ic_launcher_round" />
    <meta-data android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/white" />

我的 mipmap/ic_launcher_round/ic_launcher_round.xml:

<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
    <background android:drawable="@mipmap/ic_launcher_background"/>
    <foreground android:drawable="@mipmap/ic_launcher_foreground"/>
</adaptive-icon>

如果您双击其中的每一个,左侧列出的 I 图标在 Android Studio 中显示得很好。 (我觉得可能还有更多我不知道的东西要展示。如果您遗漏了什么,请添加评论,我会及时编辑问题。)

这是我的推送通知服务:

package my.package;

import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import java.util.Map;

public class Notifications extends FirebaseMessagingService {
    private final String TAG = "XXX-PUSHNOTES";

    public Notifications() {
        Log.d(TAG, "instantiating class!");
    }

    private void sendMessage(Map<String, String> map) {
        Intent intent = new Intent("LOAD_APP_GOING_TO_URL");
        for (Map.Entry e : map.entrySet())  {
            Log.d(TAG, "Key: " + e.getKey() + " Value: " + e.getValue());
            intent.putExtra((String) e.getKey(), (String) e.getValue());
        }
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "push notes service created");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "push notes service destroyed");
    }
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        // TODO(developer): Handle FCM messages here.
        Log.d(TAG, "From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
            Log.d(TAG, remoteMessage.getData().get("LOAD_APP_GOING_TO_URL"));
            sendMessage(remoteMessage.getData());

            if (/* Check if data needs to be processed by long running job */ true) {
                // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
                //scheduleJob();
            } else {
                // Handle message within 10 seconds
                Log.d(TAG, "Not handling the message.");
            }
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }
    }
}

这是我处理来自上述服务的广播的主要活动:

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    final String TAG = "XXX-WA-BROADCAST";
    @Override
    public void onReceive(Context context, Intent intent) {
        // Extract data included in the Intent
        String LOAD_APP_GOING_TO_URL = intent.getStringExtra("LOAD_APP_GOING_TO_URL");
        String LOAD_APP_GOING_TO_URL_TOAST_MSG = intent.getStringExtra("LOAD_APP_GOING_TO_URL_TOAST_MSG");
        Log.d(TAG, LOAD_APP_GOING_TO_URL);
        Log.d(TAG, LOAD_APP_GOING_TO_URL_TOAST_MSG);
        Toast t = Toast.makeText(getApplicationContext(), LOAD_APP_GOING_TO_URL_TOAST_MSG, Toast.LENGTH_LONG);
        t.setGravity(Gravity.TOP, 0, 0);
        t.show();
    }
};

【问题讨论】:

    标签: android push-notification icons


    【解决方案1】:

    您的代码的哪一部分发出了通知?如果我对您的代码理解正确,那么您是在使用屏幕顶部的消息而不是真正的通知来创建 toast?

    您可以根据需要创建消息:

     private Notification buildForegroundNotification(String message) {
        NotificationCompat.Builder b = new NotificationCompat.Builder(this, ChannelID);
    
        b.setOngoing(true)
                .setContentTitle("test")
                .setContentText(message)
                .setSmallIcon(1)//set there the Ressource id
                .setTicker("test");
    
        return (b.build());
    }
    

    【讨论】:

    • 当应用程序在前台收到通知时,我正在创建一个 toast 消息。当应用程序未运行或处于后台时,系统托盘会显示推送通知。这是我想更改的系统托盘中的图标。如何告诉系统使用我的图标而不是 Android 的默认图标 --- 就像其他人一样?
    • @user9654148 有什么解决办法吗?
    猜你喜欢
    • 1970-01-01
    • 2021-05-07
    • 2016-03-08
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-27
    相关资源
    最近更新 更多