{"id":1278,"date":"2023-03-18T10:05:16","date_gmt":"2023-03-18T02:05:16","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1278"},"modified":"2023-04-29T09:22:28","modified_gmt":"2023-04-29T01:22:28","slug":"android-access-umeng-push-record","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/18\/android-access-umeng-push-record\/","title":{"rendered":"Android\u53cb\u76df\u63a8\u9001\u63a5\u5165\u8e29\u5751\u8bb0\u5f55"},"content":{"rendered":"<h2>SDK\u521d\u59cb\u5316<\/h2>\n<p>\u52a1\u5fc5\u5728\u5de5\u7a0b\u7684\u81ea\u5b9a\u4e49Application\u7c7b\u7684<code>onCreate()<\/code>\u65b9\u6cd5\u4e2d\u505aSDK\u4ee3\u7801\u521d\u59cb\u5316\u5de5\u4f5c<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">\/\/\u53cb\u76df\u63a8\u9001\u521d\u59cb\u5316\n\/\/UMConfigure.init(this, &quot;Appkey&quot;, &quot;Umeng&quot;, UMConfigure.DEVICE_TYPE_PHONE, &quot;Umeng Message Secret&quot;);\nUMConfigure.init(context, &quot;******&quot;, &quot;Umeng&quot;, UMConfigure.DEVICE_TYPE_PHONE, &quot;******&quot;);<\/code><\/pre>\n<h2>\u63a8\u9001\u6d88\u606f\u63a5\u6536<\/h2>\n<h3>\u81ea\u5b9a\u4e49\u6d88\u606f\u5904\u7406Handler<\/h3>\n<pre><code class=\"language-java\">mPushAgent.setMessageHandler(MyUmengMessageHandler.getInstance(context));<\/code><\/pre>\n<pre><code class=\"language-java\">public class MyUmengMessageHandler extends UmengMessageHandler {\n    private static final String TAG = &quot;yezhou&quot;;\n    private static MyUmengMessageHandler umengMessageHandler;\n    private Context context;\n    private Handler handler;\n\n    private MyUmengMessageHandler(Context context) {\n        this.context = context;\n        handler = new Handler(context.getMainLooper());\n    }\n\n    public static MyUmengMessageHandler getInstance(Context context) {\n        synchronized (MyUmengMessageHandler.class) {\n            if (umengMessageHandler == null) {\n                synchronized (MyUmengMessageHandler.class) {\n                    umengMessageHandler = new MyUmengMessageHandler(context);\n                }\n            }\n        }\n        return umengMessageHandler;\n    }\n\n    \/**\n     * \u81ea\u5b9a\u4e49\u6d88\u606f\u7684\u56de\u8c03\u65b9\u6cd5\n     *\/\n    @Override\n    public void dealWithCustomMessage(final Context context, final UMessage msg) {\n        NLog.i(TAG, &quot;dealWithCustomMessage: &quot; + msg.message_id);\n        handler.post(new Runnable() {\n            @Override\n            public void run() {\n                \/\/ \u5bf9\u81ea\u5b9a\u4e49\u6d88\u606f\u7684\u5904\u7406\u65b9\u5f0f\uff0c\u70b9\u51fb\u6216\u8005\u5ffd\u7565\n                boolean isClickOrDismissed = true;\n                if (isClickOrDismissed) {\n                    \/\/\u81ea\u5b9a\u4e49\u6d88\u606f\u7684\u70b9\u51fb\u7edf\u8ba1\n                    UTrack.getInstance(context).trackMsgClick(msg);\n                    NLog.i(TAG, &quot;UmengMessageHandler&quot;);\n                } else {\n                    \/\/\u81ea\u5b9a\u4e49\u6d88\u606f\u7684\u5ffd\u7565\u7edf\u8ba1\n                    UTrack.getInstance(context).trackMsgDismissed(msg);\n                }\n                Toast.makeText(context, msg.custom, Toast.LENGTH_LONG).show();\n            }\n        });\n    }\n\n    \/**\n     * \u81ea\u5b9a\u4e49\u901a\u77e5\u680f\u6837\u5f0f\u7684\u56de\u8c03\u65b9\u6cd5\n     *\/\n    @Override\n    public Notification getNotification(Context context, UMessage msg) {\n        NLog.i(TAG, &quot;builder_id: &quot; + msg.builder_id);\n        switch (msg.builder_id) {\n            case 1:\n                Notification.Builder builder = new Notification.Builder(context);\n                RemoteViews myNotificationView = new RemoteViews(context.getPackageName(), R.layout.layout_notification_view);\n                myNotificationView.setTextViewText(R.id.notification_title, msg.title);\n                myNotificationView.setTextViewText(R.id.notification_text, msg.text);\n                myNotificationView.setImageViewBitmap(R.id.notification_large_icon, getLargeIcon(context, msg));\n                myNotificationView.setImageViewResource(R.id.notification_small_icon, getSmallIconId(context, msg));\n                builder.setContent(myNotificationView)\n                        .setSmallIcon(getSmallIconId(context, msg))\n                        .setTicker(msg.ticker)\n                        .setAutoCancel(true);\n\n                return builder.getNotification();\n            default:\n                \/\/\u9ed8\u8ba4\u4e3a0\uff0c\u82e5\u586b\u5199\u7684builder_id\u5e76\u4e0d\u5b58\u5728\uff0c\u4e5f\u4f7f\u7528\u9ed8\u8ba4\u3002\n                return super.getNotification(context, msg);\n        }\n    }\n}<\/code><\/pre>\n<h3>\u81ea\u5b9a\u4e49\u6d88\u606f\u63a5\u6536\u670d\u52a1<\/h3>\n<pre><code class=\"language-java\">mPushAgent.setPushIntentServiceClass(PushService.class);<\/code><\/pre>\n<pre><code class=\"language-java\">public class PushService extends UmengMessageService {\n\n    @Override\n    public void onCreate() {\n        super.onCreate();\n        NLog.i(Constants.TAG, &quot;PushService.onCreate&quot;);\n    }\n\n    @Override\n    public int onStartCommand(Intent intent, int flags, int startId) {\n        NLog.i(Constants.TAG, &quot;PushService.onStartCommand&quot;);\n        return super.onStartCommand(intent, flags, startId);\n    }\n\n    @Nullable\n    @Override\n    public IBinder onBind(Intent intent) {\n        return null;\n    }\n\n    @Override\n    public void onMessage(Context context, Intent intent) {\n        String body = intent.getStringExtra(AgooConstants.MESSAGE_BODY);\n        NLog.i(Constants.TAG, &quot;PushService.onMessage: &quot; + body); \/\/\u6d88\u606f\u4f53\n        if (TextUtils.isEmpty(body))\n            return;\n        UMessage msg;\n        try {\n            msg = new UMessage(new JSONObject(body));\n            UTrack.getInstance(this).trackMsgClick(msg);\n            Map&lt;String, String&gt; extra = msg.extra;\n            NLog.d(Constants.TAG, &quot;custom: &quot; + msg.custom); \/\/\u81ea\u5b9a\u4e49\u6d88\u606f\u7684\u5185\u5bb9\n            NLog.d(Constants.TAG, &quot;title: &quot; + msg.title); \/\/\u901a\u77e5\u6807\u9898\n            NLog.d(Constants.TAG, &quot;text: &quot; + msg.text); \/\/\u901a\u77e5\u5185\u5bb9\n            if (extra != null) {\n                \/\/\n            }\n        } catch (JSONException e) {\n            e.printStackTrace();\n        }\n    }\n\n    @Override\n    public void onDestroy() {\n        super.onDestroy();\n        NLog.i(Constants.TAG, &quot;PushService.onDestroy&quot;);\n    }\n}<\/code><\/pre>\n<h3>\u81ea\u5b9a\u4e49\u884c\u4e3a\u7684\u56de\u8c03\u5904\u7406<\/h3>\n<pre><code class=\"language-java\">\/**\n * \u81ea\u5b9a\u4e49\u884c\u4e3a\u7684\u56de\u8c03\u5904\u7406\uff0c\u53c2\u8003\u6587\u6863\uff1a\u9ad8\u7ea7\u529f\u80fd-\u901a\u77e5\u7684\u5c55\u793a\u53ca\u63d0\u9192-\u81ea\u5b9a\u4e49\u901a\u77e5\u6253\u5f00\u52a8\u4f5c\n * UmengNotificationClickHandler\u662f\u5728BroadcastReceiver\u4e2d\u88ab\u8c03\u7528\uff0c\u6545\n * \u5982\u679c\u9700\u542f\u52a8Activity\uff0c\u9700\u6dfb\u52a0Intent.FLAG_ACTIVITY_NEW_TASK\n * *\/\nUmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler() {\n    @Override\n    public void dealWithCustomAction(Context context, UMessage msg) {\n        \/\/Toast.makeText(context, msg.custom, Toast.LENGTH_LONG).show();\n        NLog.i(Constants.TAG, &quot;UmengNotificationClickHandler: &quot; + msg.message_id + msg.extra.get(&quot;cpf&quot;));\n\n        \/\/Intent intent = new Intent(MainApplication.this, MainActivity.class);\n        \/\/intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);\n\n        Map&lt;String, String&gt; extra = msg.extra;\n\n    }\n};\n\/\/\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684NotificationHandler\uff0c\u6765\u7ed3\u5408\u53cb\u76df\u7edf\u8ba1\u5904\u7406\u6d88\u606f\u901a\u77e5\n\/\/CustomNotificationHandler notificationClickHandler = new CustomNotificationHandler();\nmPushAgent.setNotificationClickHandler(notificationClickHandler);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>SDK\u521d\u59cb\u5316 \u52a1\u5fc5\u5728\u5de5\u7a0b\u7684\u81ea\u5b9a\u4e49Application\u7c7b\u7684onCreate()\u65b9\u6cd5\u4e2d\u505aSDK\u4ee3\u7801\u521d\u59cb\u5316\u5de5\u4f5c \/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[311],"tags":[322],"class_list":["post-1278","post","type-post","status-publish","format-standard","hentry","category-android-advance","tag-322"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1278","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=1278"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1278\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}