{"id":705,"date":"2020-07-17T16:12:02","date_gmt":"2020-07-17T08:12:02","guid":{"rendered":"https:\/\/badgameshow.com\/fly\/?p=705"},"modified":"2023-07-25T10:26:56","modified_gmt":"2023-07-25T02:26:56","slug":"android-notification%e9%80%9a%e7%9f%a5","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/fly\/android-notification%e9%80%9a%e7%9f%a5\/","title":{"rendered":"Android Notification \u901a\u77e5\u8a0a\u606f"},"content":{"rendered":"<h1>Android Notification \u901a\u77e5\u8a0a\u606f<\/h1>\n<ol>\n<li><a href=\"#a\">Android8.0\u4ee5\u4e0a\u4e00\u5b9a\u8981\u8a2d\u7f6eChannel\u6e20\u9053<\/a><\/li>\n<li><a href=\"#b\">\u4e00\u822c\u901a\u77e5<\/a><\/li>\n<li><a href=\"#c\">\u9032\u5ea6\u689d\u901a\u77e5<\/a><\/li>\n<li><a href=\"#d\">\u5927\u5716\u7247\u901a\u77e5<\/a><\/li>\n<li><a href=\"#e\">\u591a\u884c\u901a\u77e5<\/a><\/li>\n<li><a href=\"#f\">Github<\/a><\/li>\n<\/ol>\n<hr \/>\n<p><a id=\"a\"><\/a><\/p>\n<h3>1.Android8.0\u4ee5\u4e0a\u4e00\u5b9a\u8981\u8a2d\u7f6eChannel\u6e20\u9053<\/h3>\n<pre><code class=\"language-Kotlin line-numbers\">if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.O) {\n    val channel = NotificationChannel(packageName, \"Demo\", NotificationManager.IMPORTANCE_HIGH)\n    notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager\n    notificationManager.createNotificationChannel(channel)\n}\n<\/code><\/pre>\n<p><a id=\"b\"><\/a><\/p>\n<h3>2.\u4e00\u822c\u901a\u77e5<\/h3>\n<pre><code class=\"language-Kotlin line-numbers\">val intent = Intent(this, SecondMainActivity::class.java)\nval pendingIntent = PendingIntent.getActivity(this, 0, intent, 0)\n\nval notification = NotificationCompat.Builder(this, packageName)\n    .setContentTitle(\"\u901a\u77e5\")\n    .setContentText(\"\u6536\u5230\u4e00\u689d\u6d88\u606f\")\n    .setSmallIcon(R.drawable.logo)\n    .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.icon))\n    .setWhen(System.currentTimeMillis())\n    .setContentIntent(pendingIntent)\n    .setAutoCancel(true)\n    .build()\n\nnotificationManager.notify((0..10000).random(), notification)\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2021\/03\/20210315_163953.gif\" width=\"30%\"><\/p>\n<p><a id=\"c\"><\/a><\/p>\n<h3>3.\u9032\u5ea6\u689d\u901a\u77e5<\/h3>\n<pre><code class=\"language-Kotlin line-numbers\">val notification = NotificationCompat.Builder(this, packageName)\n    .setContentTitle(\"\u5716\u7247\u4e0b\u8f09\")\n    .setContentText(\"\u4e0b\u8f09\u4e2d\")\n    .setSmallIcon(R.drawable.logo)\n    .setWhen(System.currentTimeMillis())\n    .setStyle(\n        NotificationCompat.BigPictureStyle()\n            .bigPicture(BitmapFactory.decodeResource(resources, R.drawable.icon))\n    )\n    .setProgress(100, 0, false)\n\nthread {\n    for (i in 0..100 step 5) {\n        notification.setProgress(100, i, false)\n        notificationManager.notify(0, notification.build())\n        Thread.sleep(250)\n    }\n    notification.setProgress(0, 0, false)\n        .setContentText(\"\u4e0b\u8f09\u5b8c\u6210\")\n    notificationManager.notify(0, notification.build())\n    Thread.sleep(1000)\n    notificationManager.cancel(0)\n}\n\nnotificationManager.notify(0, notification.build())\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2021\/03\/20210315_164041.gif\" width=\"30%\"><\/p>\n<p><a id=\"d\"><\/a><\/p>\n<h3>4.\u5927\u5716\u7247\u901a\u77e5<\/h3>\n<pre><code class=\"language-Kotlin line-numbers\">val intent = Intent(this, SecondMainActivity::class.java)\nval pendingIntent = PendingIntent.getActivity(this, 0, intent, 0)\n\nval notification = NotificationCompat.Builder(this, packageName)\n    .setContentTitle(\"\u5927\u5716\u901a\u77e5\")\n    .setContentText(\"\u6536\u5230\u4e00\u689d\u6d88\u606f\")\n    .setSmallIcon(R.drawable.logo)\n    .setWhen(System.currentTimeMillis())\n    .setContentIntent(pendingIntent)\n    .setAutoCancel(true)\n    .setStyle(\n        NotificationCompat.BigPictureStyle()\n            .bigPicture(BitmapFactory.decodeResource(resources, R.drawable.icon))\n    )\n    .build()\n\nnotificationManager.notify((0..10000).random(), notification)\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2021\/03\/20210315_164131.gif\" width=\"30%\"><\/p>\n<p><a id=\"e\"><\/a><\/p>\n<h3>5.\u591a\u884c\u901a\u77e5<\/h3>\n<pre><code class=\"language-Kotlin line-numbers\">val intent = Intent(this, SecondMainActivity::class.java)\nval pendingIntent = PendingIntent.getActivity(this, 0, intent, 0)\n\nval messageList = ArrayList&lt;String&gt;();\nmessageList.add(\"\u7b2c\u4e00\u689d\u8a0a\u606f\");\nmessageList.add(\"\u7b2c\u4e8c\u689d\u8a0a\u606f\");\nmessageList.add(\"\u7b2c\u4e09\u689d\u8a0a\u606f\");\nmessageList.add(\"\u7b2c\u56db\u689d\u8a0a\u606f\");\nmessageList.add(\"\u7b2c\u4e94\u689d\u8a0a\u606f\");\n\nval inboxStyle = NotificationCompat.InboxStyle();\nmessageList.forEach {\n    inboxStyle.addLine(it);\n}\n\nval notification = NotificationCompat.Builder(this, packageName)\n    .setContentTitle(\"\u591a\u884c\u901a\u77e5\")\n    .setWhen(System.currentTimeMillis())\n    .setSmallIcon(R.drawable.logo)\n    .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.icon))\n    .setAutoCancel(true)\n    .setContentIntent(pendingIntent)\n    .setStyle(inboxStyle)\n    .build()\n\nnotificationManager.notify((0..10000).random(), notification)\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2021\/03\/20210315_164357.gif\" width=\"30%\"><\/p>\n<p><a id=\"f\"><\/a><\/p>\n<h3>6.Github<\/h3>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/github.com\/MuHongWeiWei\/NotificationDemo\" target=\"_blank\" rel=\"noopener\">Github<\/a><\/p>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Android Notification \u901a\u77e5\u8a0a\u606f Android8.0\u4ee5\u4e0a\u4e00\u5b9a\u8981\u8a2d\u7f6eChannel\u6e20\u9053 \u4e00\u822c &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[5],"tags":[13,112,111],"class_list":["post-705","post","type-post","status-publish","format-standard","hentry","category-android","tag-android","tag-channel","tag-notification"],"_links":{"self":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/705","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/comments?post=705"}],"version-history":[{"count":12,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/705\/revisions"}],"predecessor-version":[{"id":1933,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/705\/revisions\/1933"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/media?parent=705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/categories?post=705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/tags?post=705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}