【发布时间】:2019-05-23 19:11:47
【问题描述】:
所以我已经为通知实施了 FCM。 因此,我们还可以使用 Firebase Cloud Messaging 发送一些数据以及通知。 在这里,我创建了一个频道并将设备注册到“一般”主题。
这是我的代码:
包 com.femindharamshi.fcmtrial;
import android.app.NotificationManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import java.util.Map;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Map m = remoteMessage.getData();
showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), m);
Class me = remoteMessage.getClass();
Log.d("MessageDateRecieved", ""+me.toString());
}
public void showNotification(String title, String message, Map m) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MyNotifications")
.setContentTitle(title)
.setSmallIcon(R.drawable.small_logo)
.setAutoCancel(true)
.setContentText(message);
NotificationManagerCompat manager = NotificationManagerCompat.from(this);
manager.notify(999, builder.build());
}
}
现在我有以下两个问题:
- 当应用程序未运行/终止时,我的 res/drawable 文件夹中的通知徽标 (R.drawable.small_logo) 不会显示,而是显示一个灰色圆圈。我该如何解决这个问题。
- 如果我通过此消息传递数据,我可以在地图中捕获它。现在成像应用程序没有运行,我想将这些数据保存在 SharedPreferences 中,那怎么可能?
【问题讨论】:
-
由于图标的形状而显示灰色图标。尝试使用不同形状的图标,它会起作用。关于在单击通知时保存数据,与通知一起发送的值将作为意图传递给您的启动器活动,您可以使用
getextra提取它们 -
如果应用未运行,则不会调用您的服务,因此您无法创建自定义通知。
标签: android push-notification notifications firebase-cloud-messaging sharedpreferences