0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

AndroidでNavigationを使ったDeep Linkの実装Part3

Last updated at Posted at 2023-01-04

初めに

今回は、遷移情報を持った通知の作成方法を紹介していきます

本文

private fun sendNotification(
        title: String,
        body: String,
        deeplink: Deeplink?
    ) {
        val intent = Intent(this, MainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        deeplink?.let {
            intent.putDeepLink(it)
        }

        val pendingIntent = PendingIntent.getActivity(
            this,
            0,
            intent,
            PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
        )

        val channelId = "channelId"
        val notificationBuilder = NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.drawable.ic_messaging_icon)
            .setContentTitle(title)
            .setContentText(body)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)

        val notificationManager = getSystemService<NotificationManager>().requireNotNull()

        val channel = NotificationChannel(
            channelId,
            "お知らせ",
            NotificationManager.IMPORTANCE_DEFAULT
        )
        notificationManager.createNotificationChannel(channel)

        notificationManager.notify(0, notificationBuilder.build())
    }

大事なのは

val intent = Intent(this, MainActivity::class.java)
        deeplink?.let {
            intent.putDeepLink(it)
        }

この部分になります
ここでMain Activityのintent情報にDeep Linkの情報を詰め込みます

最後に

今回は通知の作成方法を紹介しました
前回の記事は下記になります
https://qiita.com/ryuji_sato/items/db6de3e447f83726a732
次の記事は下記になります
https://qiita.com/ryuji_sato/items/7e1a4289180922663782

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?