【发布时间】:2020-08-07 13:58:48
【问题描述】:
我使用 FCM 在我的 android 应用程序中设置推送通知,我使用的是 FCM 数据而不是通知。我添加了唯一 ID 以从我的服务器推送消息,但是在我的应用程序中,当新通知到来时,它会自动清除我之前的通知。 我的推送 .php 代码
class Push {
//notification title
private $title;
//notification message
private $message;
//notification image url
private $image;
private $notId;
//initializing values in this constructor
function __construct($title, $message, $image,$notId,$sound) {
$this->title = $title;
$this->message = $message;
$this->image = $image;
$this->notId = $notId;
$this->sound = $sound;
}
//getting the push notification
public function getPush() {
$res = array();
$res['title'] = $this->title;
$res['body'] = $this->message;
$res['image'] = $this->image;
$res['notId'] = $this->notId;
$res['sound'] = $this->sound;
return $res;
}
我的应用程序中的 firebase 消息服务
lass myfirebasemessaging : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage?) {
super.onMessageReceived(remoteMessage)
if (remoteMessage!!.data != null) {
val title = remoteMessage.data!!.get("title")
val body = remoteMessage.data!!.get("body")
val tag = remoteMessage.data!!.get("notId")
NotificationHelper.displayNotification(applicationContext, title!!, body!!, tag!!)
}
}
通知助手
fun displayNotification(context: Context, title: String, body: String, notId: String) {
val intent = Intent(context, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(
context,
100,
intent,
PendingIntent.FLAG_CANCEL_CURRENT
)
val mBuilder = NotificationCompat.Builder(context, MainActivity.CHANNEL_ID)
.setSmallIcon(R.drawable.ic_waterlogo)
.setContentTitle(title)
.setContentText(body)
.setStyle(NotificationCompat.BigTextStyle()
.bigText(body))
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(Notification.FLAG_ONLY_ALERT_ONCE)
val mNotificationMgr = NotificationManagerCompat.from(context)
val mp: MediaPlayer = MediaPlayer.create(context, R.raw.notification)
mp.start()
mNotificationMgr.notify(1, mBuilder.build())
}
就像我无法为通知设置任何样式一样。我需要在我的 fcm 服务中添加任何其他内容吗?如何在其中使用唯一 ID 来不清除之前的消息。
【问题讨论】:
标签: php android firebase kotlin push-notification