android消息推送(Jpush)

一.我采用极光推送Jpush进行消息推送,完成一定时间给应用发送消息

二.开发步骤

1.下载Jpush的SDK

2.注册用户和应用,获取APPKey和 Master Secret

 3-1.将SDK的libs文件拷贝到工程libs下,并加入 build path

3-2.复制 res/ 中drawable-hdpi, layout, values文件夹中的资源文件到你的工程中 res/ 对应同名的目录

4.替换工程的清单文件为SDK的清单文件(或者配置你的AndroidManifest.xml文件步骤如下)

  • 复制备注为 "Required" 的部分
  • 将标注为“您应用的包名”的部分,替换为当前应用程序的包名
  • 将标注为“您应用的Appkey”的部分,替换为在Portal上注册该应用的的Key,例如:9fed5bcb7b9b87413678c407
  • 注册广播接收者,并在清单文件配置
  • package com.example.push;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    
    public class MyReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            System.out.println("我接收到广播的消息了");
    
        }
    
    }
  • 修改前清单文件
  •   1 <?xml version="1.0" encoding="utf-8"?>
      2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
      3     package="您应用的包名"
      4     android:versionCode="303"
      5     android:versionName="3.0.3"
      6     >
      7     <uses-sdk android:minSdkVersion="9" android:targetSdkVersion="23" />
      8 
      9     <!-- Required -->
     10     <permission 
     11         android:name="您应用的包名.permission.JPUSH_MESSAGE"  
     12         android:protectionLevel="signature" />
     13 
     14     <!-- Required -->
     15     <uses-permission android:name="您应用的包名.permission.JPUSH_MESSAGE" />
     16     <uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />
     17     <uses-permission android:name="android.permission.INTERNET" />
     18     <uses-permission android:name="android.permission.WAKE_LOCK" />
     19     <uses-permission android:name="android.permission.READ_PHONE_STATE" />
     20     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     21     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
     22     <uses-permission android:name="android.permission.VIBRATE" />
     23     <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
     24     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
     25     <uses-permission android:name="android.permission.WRITE_SETTINGS" /> 
     26     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
     27 
     28     <!-- Optional. Required for location feature -->
     29     <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <!-- 用于开启 debug 版本的应用在6.0 系统上 层叠窗口权限 -->
     30     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
     31     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
     32     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
     33     <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
     34     <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
     35     <uses-permission android:name="android.permission.GET_TASKS" />
     36 
     37     <application
     38         android:icon="@drawable/ic_launcher"
     39         android:label="@string/app_name"
     40         android:name="Your Application Name">
     41 
     42         <!-- Required SDK 核心功能-->
     43         <!-- 可配置android:process参数将PushService放在其他进程中 -->
     44         <service
     45             android:name="cn.jpush.android.service.PushService"
     46             android:enabled="true"
     47             android:exported="false" >
     48             <intent-filter>
     49                 <action android:name="cn.jpush.android.intent.REGISTER" />
     50                 <action android:name="cn.jpush.android.intent.REPORT" />
     51                 <action android:name="cn.jpush.android.intent.PushService" />
     52                 <action android:name="cn.jpush.android.intent.PUSH_TIME" />
     53             </intent-filter>
     54         </service>
     55 
     56         <!-- since 1.8.0 option 可选项。用于同一设备中不同应用的JPush服务相互拉起的功能。 -->
     57         <!-- 若不启用该功能可删除该组件,将不拉起其他应用也不能被其他应用拉起 -->
     58          <service
     59              android:name="cn.jpush.android.service.DaemonService"
     60              android:enabled="true"
     61              android:exported="true">
     62              <intent-filter >
     63                  <action android:name="cn.jpush.android.intent.DaemonService" />
     64                  <category android:name="您应用的包名"/>
     65              </intent-filter>
     66          </service>
     67 
     68         <!-- Required SDK核心功能-->
     69         <receiver
     70             android:name="cn.jpush.android.service.PushReceiver"
     71             android:enabled="true" >
     72           <intent-filter android:priority="1000"> 
     73                 <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" /> 
     74                 <category android:name="您应用的包名"/> 
     75             </intent-filter>
     76             <intent-filter>
     77                 <action android:name="android.intent.action.USER_PRESENT" />
     78                 <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
     79             </intent-filter>
     80             <!-- Optional -->
     81             <intent-filter>
     82                 <action android:name="android.intent.action.PACKAGE_ADDED" />
     83                 <action android:name="android.intent.action.PACKAGE_REMOVED" />
     84                 <data android:scheme="package" />
     85             </intent-filter>
     86         </receiver>
     87 
     88         <!-- Required SDK核心功能-->
     89         <activity
     90             android:name="cn.jpush.android.ui.PushActivity"
     91             android:configChanges="orientation|keyboardHidden"
     92             android:theme="@android:style/Theme.NoTitleBar"
     93             android:exported="false" >
     94             <intent-filter>
     95                 <action android:name="cn.jpush.android.ui.PushActivity" />
     96                 <category android:name="android.intent.category.DEFAULT" />
     97                 <category android:name="您应用的包名" />
     98             </intent-filter>
     99         </activity>
    100         <!-- SDK核心功能-->
    101         <activity
    102             android:name="cn.jpush.android.ui.PopWinActivity"
    103             android:configChanges="orientation|keyboardHidden"
    104             android:exported="false"
    105             android:theme="@style/MyDialogStyle">
    106             <intent-filter>
    107                 <category android:name="android.intent.category.DEFAULT" />
    108                 <category android:name="您应用的包名" />
    109             </intent-filter>
    110         </activity>
    111 
    112         <!-- Required SDK核心功能-->
    113         <service
    114             android:name="cn.jpush.android.service.DownloadService"
    115             android:enabled="true"
    116             android:exported="false" >
    117         </service>
    118 
    119         <!-- Required SDK核心功能-->
    120         <receiver android:name="cn.jpush.android.service.AlarmReceiver" />
    121 
    122         <!-- User defined. 用户自定义的广播接收器-->
    123          <receiver
    124              android:name="您自己定义的Receiver"
    125              android:enabled="true">
    126              <intent-filter>
    127                  <!--Required 用户注册SDK的intent-->
    128                  <action android:name="cn.jpush.android.intent.REGISTRATION" /> 
    129                  <!--Required 用户接收SDK消息的intent--> 
    130                  <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> 
    131                  <!--Required 用户接收SDK通知栏信息的intent-->
    132                  <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> 
    133                  <!--Required 用户打开自定义通知栏的intent-->
    134                  <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> 
    135                  <!-- 接收网络变化 连接/断开 since 1.6.3 -->
    136                  <action android:name="cn.jpush.android.intent.CONNECTION" />
    137                  <category android:name="您应用的包名" />
    138              </intent-filter>
    139          </receiver>
    140 
    141         <!-- Required. For publish channel feature -->
    142         <!-- JPUSH_CHANNEL 是为了方便开发者统计APK分发渠道。-->
    143         <!-- 例如: -->
    144         <!-- 发到 Google Play 的APK可以设置为 google-play; -->
    145         <!-- 发到其他市场的 APK 可以设置为 xxx-market。 -->
    146         <!-- 渠道统计报表位于控制台页面的 “统计”-“用户统计”-“渠道分布” 中-->
    147         <meta-data android:name="JPUSH_CHANNEL" android:value="developer-default"/>
    148         <!-- Required. AppKey copied from Portal -->
    149         <meta-data android:name="JPUSH_APPKEY" android:value="您应用的Appkey"/> 
    150     </application>
    151 </manifest>

    修改后清单文件:

  •   1 <?xml version="1.0" encoding="utf-8"?>
      2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
      3     package="com.example.push"
      4     android:versionCode="1"
      5     android:versionName="1.0" >
      6 
      7     <uses-sdk
      8         android:minSdkVersion="14"
      9         android:targetSdkVersion="18" />
     10         <!-- Required -->
     11     <permission
     12         android:name="com.example.push.permission.JPUSH_MESSAGE"
     13         android:protectionLevel="signature" />
     14   
     15     <!-- Required  一些系统要求的权限,如访问网络等-->
     16     <uses-permission android:name="com.example.push.permission.JPUSH_MESSAGE" />
     17     <uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />
     18     <uses-permission android:name="android.permission.INTERNET" />
     19     <uses-permission android:name="android.permission.WAKE_LOCK" />
     20     <uses-permission android:name="android.permission.READ_PHONE_STATE" />
     21     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     22     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
     23     <uses-permission android:name="android.permission.WRITE_SETTINGS" />
     24     <uses-permission android:name="android.permission.VIBRATE" />
     25     <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
     26     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
     27     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
     28         <!-- Optional for location -->
     29     <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <!-- 用于开启 debug 版本的应用在6.0 系统上 层叠窗口权限 -->
     30     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
     31     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
     32     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
     33     <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
     34     <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
     35     <uses-permission android:name="android.permission.GET_TASKS" />
     36 
     37     <application
     38         android:allowBackup="true"
     39         android:icon="@drawable/ic_launcher"
     40         android:label="@string/app_name"
     41         android:theme="@style/AppTheme" >
     42         <activity
     43             android:name=".MainActivity"
     44             android:label="@string/app_name" >
     45             <intent-filter>
     46                 <action android:name="android.intent.action.MAIN" />
     47 
     48                 <category android:name="android.intent.category.LAUNCHER" />
     49             </intent-filter>
     50         </activity>
     51         
     52                <!-- Rich push 核心功能 since 2.0.6-->
     53         <activity
     54             android:name="cn.jpush.android.ui.PopWinActivity"
     55             android:theme="@style/MyDialogStyle"
     56             android:exported="false">
     57         </activity>
     58 
     59         <!-- Required SDK核心功能-->
     60         <activity
     61             android:name="cn.jpush.android.ui.PushActivity"
     62             android:configChanges="orientation|keyboardHidden"
     63             android:theme="@android:style/Theme.NoTitleBar"
     64             android:exported="false">
     65             <intent-filter>
     66                 <action android:name="cn.jpush.android.ui.PushActivity" />
     67                 <category android:name="android.intent.category.DEFAULT" />
     68                 <category android:name="com.example.push" />
     69             </intent-filter>
     70         </activity>
     71 
     72         <!-- Required SDK 核心功能-->
     73         <!-- 可配置android:process参数将PushService放在其他进程中 -->
     74         <service
     75             android:name="cn.jpush.android.service.PushService"
     76             android:process=":mult"
     77             android:exported="false">
     78             <intent-filter>
     79                 <action android:name="cn.jpush.android.intent.REGISTER" />
     80                 <action android:name="cn.jpush.android.intent.REPORT" />
     81                 <action android:name="cn.jpush.android.intent.PushService" />
     82                 <action android:name="cn.jpush.android.intent.PUSH_TIME" />
     83             </intent-filter>
     84         </service>
     85         
     86                <!-- Required SDK核心功能-->
     87         <receiver
     88             android:name="cn.jpush.android.service.PushReceiver"
     89             android:enabled="true"
     90             android:exported="false">
     91             <intent-filter android:priority="1000">
     92                 <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" />   <!--Required  显示通知栏 -->
     93                 <category android:name="com.example.push" />
     94             </intent-filter>
     95             <intent-filter>
     96                 <action android:name="android.intent.action.USER_PRESENT" />
     97                 <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
     98             </intent-filter>
     99             <!-- Optional -->
    100             <intent-filter>
    101                 <action android:name="android.intent.action.PACKAGE_ADDED" />
    102                 <action android:name="android.intent.action.PACKAGE_REMOVED" />
    103 
    104                 <data android:scheme="package" />
    105             </intent-filter>
    106         </receiver>
    107 
    108         <!-- Required SDK核心功能-->
    109         <receiver android:name="cn.jpush.android.service.AlarmReceiver" android:exported="false"/>
    110 
    111         <!-- User defined.  For test only  用户自定义的广播接收器-->
    112         <receiver
    113             android:name="com.example.push.MyReceiver"
    114             android:exported="false"
    115             android:enabled="true">
    116             <intent-filter>
    117                 <action android:name="cn.jpush.android.intent.REGISTRATION" /> <!--Required  用户注册SDK的intent-->
    118                 <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <!--Required  用户接收SDK消息的intent-->
    119                 <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> <!--Required  用户接收SDK通知栏信息的intent-->
    120                 <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <!--Required  用户打开自定义通知栏的intent-->
    121                 <action android:name="cn.jpush.android.intent.CONNECTION" /><!-- 接收网络变化 连接/断开 since 1.6.3 -->
    122                 <category android:name="com.example.push" />
    123             </intent-filter>
    124         </receiver>
    125 
    126         <!-- Required  . Enable it you can get statistics data with channel -->
    127         <meta-data android:name="JPUSH_CHANNEL" android:value="developer-default"/>
    128         <meta-data android:name="JPUSH_APPKEY" android:value="0ebc27f7f413b62274134d4a" /> <!--  </>值来自开发者平台取得的AppKey-->
    129         
    130     </application>
    131 
    132 </manifest>

    5.推送设置

  • 三.API使用:JPush SDK 提供的 API 接口,都主要集中在 cn.jpush.android.api.JPushInterface 类里
  • 1自定义Application类:MyApplication,并在onCreate(执行一次)方法中调用
    public static void init(Context context)初始化
  •  public static void setDebugMode(boolean debugEnalbed)配置调试模式
  • 代码如下:
  • package com.example.push;
    
    import android.app.Application;
    import cn.jpush.android.api.JPushInterface;
    
    public class MyApplication extends Application {
        
        @Override
        public void onCreate() {
            // TODO Auto-generated method stub
            super.onCreate();
            JPushInterface.setDebugMode(true);
            JPushInterface.init(this);
        }
    
    }

  • 2.测试确认

  • 确认所需的权限都已经添加。如果必须的权限未添加,日志会提示错误。
  • 确认 AppKey(在Portal上生成的)已经正确的写入 Androidmanifest.xml 。
  • 确认在程序启动时候调用了init(context) 接口
  • 确认测试手机(或者模拟器)已成功连入网络 + 客户端调用 init 后不久,如果一切正常,应有登录成功的日志信息
    • 启动应用程序,在 Portal 上向应用程序发送自定义消息或者通知栏提示。详情请参考管理Portal
      • 在几秒内,客户端应可收到下发的通知或者正定义消息,如果 SDK 工作正常,则日志信息会如下:
    [JPushInterface] action:init
    
    .......
    
    [PushService] Login succeed!
  • 发送消息
  • 效果查看
  • 可以推送时附带信息:

  • 接收广播如下:

  • package com.example.push;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import cn.jiguang.api.JCoreInterface;
    import cn.jpush.android.api.JPushInterface;
    
    public class MyReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            
            Log.e("MyReceiver", "我接收到广播的消息了");
            //获取接收到推送的消息数据
            Bundle bundle = intent.getExtras();
            String JSON = bundle.getString(JPushInterface.EXTRA_EXTRA);//得到的是服务器返回的json串,需要解析才能得到消息内容
            /*
             *  {"n_builder_id":0,"ad_id":"1218881444","n_only":1,"m_content":{"n_content":"锄禾日当午,android真他妈难。","n_style":0,"n_extras":{},"ad_t":0,"n_priority":1,"n_alert_type":7,"n_title":"","n_category":"","n_flag":1},"show_type":4} - JResponse{code=0}
    
             */
            
            Log.e("MyReceiver",JSON);
        }
    
    }
  • 清单文件里配置只在点击时发送广播,其他情况全部注释
  • 点击通知消息时运行效果:

  •  如果想应用推出后不推送消息,则可以在Activity销毁时停止服务

  • 源码下载地址:https://download.csdn.net/download/xipengbozai/15994898?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522161771641716780265433706%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fdownload.%2522%257D&request_id=161771641716780265433706&biz_id=1&utm_medium=distribute.pc_search_result.none-task-download-2~download~first_rank_v2~rank_dl_v1-1-15994898.pc_v2_rank_dl_v1&utm_term=%E6%B6%88%E6%81%AF
需要程序源码的可以加我微信x241602私聊。
原文地址:https://www.cnblogs.com/huipengbo/p/7048009.html