{"id":519,"date":"2015-11-29T17:40:21","date_gmt":"2015-11-29T09:40:21","guid":{"rendered":"https:\/\/www.ray650128.com\/wordpress\/?p=519"},"modified":"2021-08-26T23:10:18","modified_gmt":"2021-08-26T15:10:18","slug":"android%e9%a1%af%e7%a4%ba%e9%80%9a%e7%9f%a5","status":"publish","type":"post","link":"https:\/\/blog.ray650128.com\/?p=519","title":{"rendered":"[Android]Notification\u63a8\u64ad\u901a\u77e5"},"content":{"rendered":"\n<p>\u7576LINE\u6536\u5230\u597d\u53cb\u767c\u51fa\u7684\u8a0a\u606f\uff0c\u6216\u662f\u6536\u5230Email\uff0c\u5728\u87a2\u5e55\u7684\u4e0a\u65b9\u6216\u662f\u7cfb\u7d71\u7684\u901a\u77e5\u6e05\u55ae\u986f\u793a\u4e00\u500b\u63d0\u793a\u65b9\u584a\u3002<\/p>\n\n\n\n<p>\u9019\u500b\u8a0a\u606f\u65b9\u584a\u5c31\u53eb\u505a\u63a8\u64ad\u901a\u77e5\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5982\u4f55\u5efa\u7acb\u901a\u77e5<\/h2>\n\n\n\n<p>\u9019\u88e1\u4ee5\u6309\u4e0bAPP\u4e0a\u7684\u6309\u9215\u767c\u51fa\u63a8\u64ad\u901a\u77e5\u505a\u70ba\u793a\u7bc4<\/p>\n\n\n\n<p>1.\u9996\u5148\u5728\u4f48\u5c40\u6587\u4ef6\u4e2d\u653e\u5165\u4e00\u500b\u6309\u9215\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"xml\" class=\"language-xml line-numbers\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?>\n\n&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n\u00a0   xmlns:ads=\"http:\/\/schemas.android.com\/apk\/lib\/com.google.ads\"\n\u00a0   android:layout_width=\"fill_parent\"\n\u00a0   android:layout_height=\"fill_parent\"\n\u00a0   android:orientation=\"vertical\" >\n\n\u00a0   &lt;Button\n\u00a0       android:id=\"@+id\/button\"\n\u00a0       android:layout_width=\"wrap_content\"\n\u00a0       android:layout_height=\"wrap_content\"\n\u00a0       android:layout_weight=\"1\"\n\u00a0       android:text=\"Button\" \/>\n\n&lt;\/LinearLayout><\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>2.\u63a5\u8457\u5728Activity\u4e2d\u52a0\u5165\u6309\u9215\uff0c\u4ee5\u53caOnClickListener\u548c\u63a8\u64ad\u901a\u77e5\u5143\u4ef6\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java line-numbers\">public class MainActivity extends Activity {\n\n    int ID_NOTIFICATION = 0x00;\n \n\u00a0   @Override\n\u00a0   protected void onCreate(Bundle savedInstanceState) {\n\u00a0       super.onCreate(savedInstanceState);\n\u00a0       setContentView(R.layout.activity_main);\n\n        Button button = (Button) findViewById(R.id.button);\n        button.setOnClickListener(new OnClickListener() {\n            @Override\n            public void onClick(View view) {\n                showNotification();\n            }\n        });\n    }\n\n    private void showNotification() {\n        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);\n\n        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.url_shoeisha)));\n        PendingIntent contentIntent = PendingIntent.getActivity(this, ID_NOTIFICATION, intent, PendingIntent.FLAG_UPDATE_CURRENT);\n\n        \/\/ \u5ba3\u544aNotification\u7269\u4ef6\n        Notification.Builder notification = new Notification.Builder(this);\n        \/\/ \u8a2d\u5b9a\u6642\u9593\u6233\u8a18\n        notification.setWhen(System.currentTimeMillis());\n        \/\/ \u8a2d\u5b9aIntent\n        notification.setContentIntent(contentIntent);\n        \/\/ \u8a2d\u5b9a\u5716\u793a\n        notification.setSmallIcon(android.R.drawable.stat_notify_chat);\n        \/\/ \u8a2d\u5b9a\u6587\u5b57\n        notification.setTicker(\"\u6211\u662f\u5167\u5bb9\");\n        \/\/ \u8a2d\u5b9a\u6a19\u984c\n        notification.setContentTitle(\"\u6211\u662f\u6a19\u984c\");\n        \/\/ \u8a2d\u5b9a\u63d0\u793a\uff08\u6307\u793a\u71c8\u3001\u63d0\u793a\u97f3\u3001\u9707\u52d5\uff09\n        notification.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);\n        \/\/ \u9ede\u9078\u5f8c\u81ea\u52d5\u6e05\u9664\n        notification.setAutoCancel(true);\n        Notification notification = notification.build();\n\n        \/\/ \u986f\u793a\u901a\u77e5\n        notificationManager.notify(ID_NOTIFICATION_SAMPLE_ACTIVITY, notification);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u662f\u751f\u6210\u63a8\u64ad\u901a\u77e5\u7684\u793a\u7bc4<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7576LINE\u6536\u5230\u597d\u53cb\u767c\u51fa\u7684\u8a0a\u606f\uff0c\u6216\u662f\u6536\u5230Email\uff0c\u5728\u87a2\u5e55\u7684\u4e0a\u65b9\u6216\u662f\u7cfb\u7d71\u7684\u901a\u77e5\u6e05\u55ae\u986f\u793a\u4e00\u500b\u63d0\u793a\u65b9\u584a\u3002 \u9019\u500b\u8a0a\u606f\u65b9 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":299,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[10,17],"class_list":["post-519","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-2","tag-android-app","tag-java"],"_links":{"self":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts\/519","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=519"}],"version-history":[{"count":2,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts\/519\/revisions"}],"predecessor-version":[{"id":1165,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts\/519\/revisions\/1165"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/"}],"wp:attachment":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}