{"id":1267,"date":"2023-03-18T09:56:35","date_gmt":"2023-03-18T01:56:35","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1267"},"modified":"2023-04-29T09:24:30","modified_gmt":"2023-04-29T01:24:30","slug":"android-access-firebase-push-notification","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/18\/android-access-firebase-push-notification\/","title":{"rendered":"Android\u63a5\u5165Firebase\u63a8\u9001\u901a\u77e5"},"content":{"rendered":"<p>\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/firebase.google.com\/docs\/cloud-messaging\/android\/client\">https:\/\/firebase.google.com\/docs\/cloud-messaging\/android\/client<\/a><br \/>\n\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/firebase\/quickstart-android\/blob\/master\/messaging\/app\/src\/main\/java\/com\/google\/firebase\/quickstart\/fcm\/\">https:\/\/github.com\/firebase\/quickstart-android\/blob\/master\/messaging\/app\/src\/main\/java\/com\/google\/firebase\/quickstart\/fcm\/<\/a><\/p>\n<h2>Firebase\u63a7\u5236\u53f0\u914d\u7f6e<\/h2>\n<p>\u5728Firebase\u63a7\u5236\u53f0\u521b\u5efa\u9879\u76ee\uff0c\u5e76\u914d\u7f6eSHA1\uff0c\u7136\u540e\u4e0b\u8f7dgoogle-service.json\u6587\u4ef6\u5e76\u6dfb\u52a0\u5230\u9879\u76ee\u7684app\u76ee\u5f55\u4e0b\u5373\u53ef<\/p>\n<p><!-- more --><\/p>\n<h2>Gradle\u6dfb\u52a0\u4f9d\u8d56<\/h2>\n<pre><code>implementation &#039;com.google.firebase:firebase-messaging:17.3.4&#039;<\/code><\/pre>\n<h2>\u521b\u5efaFirebaseMessagingService\u670d\u52a1\u5e76\u6ce8\u518c<\/h2>\n<pre><code class=\"language-java\">public class MyFirebaseMessagingService extends FirebaseMessagingService {\n    \/**\n     * Called if InstanceID token is updated. This may occur if the security of the previous token had been compromised.\n     * Note that this is called when the InstanceID token is initially generated so this is where you would retrieve the token.\n     *\/\n    @Override\n    public void onNewToken(String token) {\n        NLog.i(Constants.TAG, &quot;Refreshed token: &quot; + token);\n\n        \/\/ If you want to send messages to this application instance or manage this apps subscriptions on the server side,\n        \/\/ send the Instance ID token to your app server.\n        \/\/ sendRegistrationToServer(token);\n    }\n\n    @Override\n    public void onMessageReceived(RemoteMessage remoteMessage) {\n        super.onMessageReceived(remoteMessage);\n\n        NLog.i(Constants.TAG, &quot;From: &quot; + remoteMessage.getFrom());\n\n        \/\/ Check if message contains a data payload.\n        if (remoteMessage.getData().size() &gt; 0) {\n            NLog.i(Constants.TAG, &quot;Message data payload: &quot; + remoteMessage.getData());\n        }\n\n        \/\/ Check if message contains a notification payload.\n        if (remoteMessage.getNotification() != null) {\n            NLog.i(Constants.TAG, &quot;Message Notification Body: &quot; + remoteMessage.getNotification().getBody());\n        }\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-xml\">&lt;service android:name=&quot;.service.MyFirebaseMessagingService&quot;&gt;\n    &lt;intent-filter&gt;\n        &lt;action android:name=&quot;com.google.firebase.MESSAGING_EVENT&quot; \/&gt;\n    &lt;\/intent-filter&gt;\n&lt;\/service&gt;<\/code><\/pre>\n<h2>\u83b7\u53d6\u5ba2\u6237\u7aefToken<\/h2>\n<pre><code class=\"language-java\">FirebaseInstanceId.getInstance().getInstanceId()\n    .addOnCompleteListener(new OnCompleteListener&lt;InstanceIdResult&gt;() {\n        @Override\n        public void onComplete(@NonNull Task&lt;InstanceIdResult&gt; task) {\n            if (!task.isSuccessful()) {\n                NLog.w(TAG, &quot;GetInstanceId failed: &quot;, task.getException());\n                return;\n            }\n            \/\/ Get new Instance ID token\n            String token = task.getResult().getToken();\n            NLog.d(TAG, &quot;Firebase Token: &quot; + token);\n        }\n    });<\/code><\/pre>\n<h2>\u6ce8\u518cTopic<\/h2>\n<pre><code class=\"language-java\">FirebaseMessaging.getInstance().subscribeToTopic(&quot;activity&quot;)\n    .addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {\n        @Override\n        public void onComplete(@NonNull Task&lt;Void&gt; task) {\n            if (!task.isSuccessful()) {\n                NLog.i(TAG, &quot;Subscribe topic failure&quot;);\n            } else {\n                NLog.i(TAG, &quot;Subscribe topic success&quot;);\n            }\n        }\n    });<\/code><\/pre>\n<h2>\u63a7\u5236\u53f0\u53d1\u9001\u6d88\u606f<\/h2>\n<p>\u5728\u63a7\u5236\u53f0\u5de6\u4fa7\u9009\u62e9<code>Cloud Messaging<\/code>\uff0c\u7136\u540e\u70b9\u51fb\u5199\u65b0\u6d88\u606f\uff0c\u5373\u53ef\u5411\u6307\u5b9a\u8bbe\u5907\u6216\u5168\u90e8\u8bbe\u5907\u53d1\u9001\u6d88\u606f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53c2\u8003\uff1ahttps:\/\/firebase.google.com\/docs\/cloud-messaging\/and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[316],"class_list":["post-1267","post","type-post","status-publish","format-standard","hentry","category-android-basic","tag-firebase"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1267","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1267"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1267\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}