繁体   English   中英

firebase 推送通知自动清除之前的通知

[英]firebase push-notification automatically clear previous notification

我已经使用 FCM 在我的 android 应用程序中设置推送通知,并且我使用的是 FCM 数据而不是通知。 我已经添加了唯一 ID 以从我的服务器推送消息,但是在我的应用程序中,当新通知到来时,它会自动清除我以前的通知。 我的 push.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 以不清除先前的消息。

将此行中的 1 替换为唯一 ID。 mNotificationMgr.notify(1, mBuilder.build())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM