【发布时间】:2020-01-04 06:25:44
【问题描述】:
我新实现了 Firebase 云消息传递。
我已经能够向我的测试设备发送测试消息。但是,通过广告系列发送通知并不走运。
这是我如何扩展我的FirebaseMessagingService:
class MyFirebaseMessagingService : FirebaseMessagingService() {
private val CHANNEL_ID = "YB_NOTIFICATION_CHANNEL"
var notification: Notification? = null
private val TAG = "MessagingService"
private val remote_picture: Bitmap? = null
override fun onMessageReceived(remoteMessage: RemoteMessage) {
Log.d(TAG, "From: " + remoteMessage.from)
Log.d(TAG, "Notification Message Body: " + remoteMessage.notification!!.body.toString())
Log.d(TAG, remoteMessage.from)
sendNotification(remoteMessage.notification!!.body, remoteMessage.notification!!.title)
}
private fun sendNotification(messageBody: String?, title: String?) {
val intent = Intent(this, NotificationActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent =
PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
val defaultSoundUri =
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALL)
val icon = BitmapFactory.decodeResource(this.resources, R.drawable.launcher_round)
val notificationBuilder =
NotificationCompat.Builder(
this,
CHANNEL_ID
)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setContentText(messageBody)
.setContentTitle(resources.getString(R.string.app_name))
.setLargeIcon(icon)
.setSmallIcon(notificationIcon)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
notification = notificationBuilder.build()
val manager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
manager.notify(1, notification)
MyNotificationManager.getInstance(this).displayNotification(title, messageBody)
}
private val notificationIcon: Int
get() {
val useWhiteIcon = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
return if (useWhiteIcon) R.mipmap.mypush else R.mipmap.mypush
}
}
下面是MyNotificationManager类:
public class MyNotificationManager {
private Context mCtx;
private static MyNotificationManager mInstance;
private MyNotificationManager(Context context) {
mCtx = context;
}
public static synchronized MyNotificationManager getInstance(Context context) {
if (mInstance == null) {
mInstance = new MyNotificationManager(context);
}
return mInstance;
}
public void displayNotification(String title, String body) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(mCtx, AppConstants.CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle(title)
.setContentText(body);
Intent resultIntent = new Intent(mCtx, Notifications.class);
PendingIntent pendingIntent = PendingIntent.getActivity(mCtx, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
NotificationManager mNotifyMgr =
(NotificationManager) mCtx.getSystemService(NOTIFICATION_SERVICE);
if (mNotifyMgr != null) {
mNotifyMgr.notify(1, mBuilder.build());
}
}
}
最后,这是 Manifest 中声明的服务。
<service
android:name="._services.broadcast.MyFirebaseMessagingService"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
我还搜索了一些关于 SO 的问题,但我看到的是人们提到了 FirebaseInstanceId 以及为此提供的服务。较新的教程甚至 Firebase Cloud Messaging 设置文档本身都没有提到这一点。
我想知道我哪里出错了。
此外,即使我的应用程序未打开,我也能够成功接收来自 Notification composer 的有针对性的测试消息。
【问题讨论】:
标签: android firebase kotlin firebase-cloud-messaging