【发布时间】: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