Android中实现消息推送(JPush)-极光推送

1,去JPush官网注册一个账号,创建你的app的应用,并且拿到你应用的AppKey

2,在JPush官网下载对应的sdk,解压出来,将libs文件下的所有的文件全部复制到你工程的libs文件中

3,在清单文件中添加对应的权限和activity(更改对应的包名和对应的AppKey)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
AndroidManifest.xml权限配置:
<?xml version= "1.0"  encoding= "utf-8" ?>
<manifest xmlns:android= "http://schemas.android.com/apk/res/android"
     package = "您应用的包名"
     android:versionCode= "100"
     android:versionName= "1.0.0"
     >
     <uses-sdk android:minSdkVersion= "11"  android:targetSdkVersion= "17"  />
 
     <!-- Required -->
     <permission android:name= "您应用的包名.permission.JPUSH_MESSAGE"  android:protectionLevel= "signature"  />
 
     <!-- Required -->
     <uses-permission android:name= "You Package.permission.JPUSH_MESSAGE"  />
     <uses-permission android:name= "android.permission.RECEIVE_USER_PRESENT"  />
     <uses-permission android:name= "android.permission.INTERNET"  />
     <uses-permission android:name= "android.permission.WAKE_LOCK"  />
     <uses-permission android:name= "android.permission.READ_PHONE_STATE"  />
     <uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE"  />
     <uses-permission android:name= "android.permission.READ_EXTERNAL_STORAGE"  />
     <uses-permission android:name= "android.permission.VIBRATE"  />
     <uses-permission android:name= "android.permission.MOUNT_UNMOUNT_FILESYSTEMS"  />
     <uses-permission android:name= "android.permission.ACCESS_NETWORK_STATE"  />
     <uses-permission android:name= "android.permission.WRITE_SETTINGS"  />
 
     <!-- Optional. Required  for  location feature -->
     <uses-permission android:name= "android.permission.ACCESS_COARSE_LOCATION"  />
     <uses-permission android:name= "android.permission.ACCESS_WIFI_STATE"  />
     <uses-permission android:name= "android.permission.CHANGE_WIFI_STATE"  />
     <uses-permission android:name= "android.permission.ACCESS_FINE_LOCATION"  />
     <uses-permission android:name= "android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"  />
     <uses-permission android:name= "android.permission.CHANGE_NETWORK_STATE"  />
 
应用包名及appkey替换:   
     <application
         android:icon= "@drawable/ic_launcher"
         android:label= "@string/app_name"
         android:name= "Your Application" >
 
         <!-- Required SDK 核心功能-->
         <!-- option since  2.0 . 5  可配置PushService,DaemonService,PushReceiver,AlarmReceiver的android:process参数 将JPush相关组件设置为一个独立进程 -->
         <!-- 如:android:process= ":remote"  -->
         <service
             android:name= "cn.jpush.android.service.PushService"
             android:enabled= "true"
             android:exported= "false"  >
             <intent-filter>
                 <action android:name= "cn.jpush.android.intent.REGISTER"  />
                 <action android:name= "cn.jpush.android.intent.REPORT"  />
                 <action android:name= "cn.jpush.android.intent.PushService"  />
                 <action android:name= "cn.jpush.android.intent.PUSH_TIME"  />
             </intent-filter>
         </service>
 
         <!-- since  1.8 . 0  option 可选项。用于同一设备中不同应用的JPush服务相互拉起的功能。 -->
         <!-- 若不启用该功能可删除该组件,将不拉起其他应用也不能被其他应用拉起 -->
          <service
              android:name= "cn.jpush.android.service.DaemonService"
              android:enabled= "true"
              android:exported= "true" >
              <intent-filter >
                  <action android:name= "cn.jpush.android.intent.DaemonService"  />
                  <category android:name= "您应用的包名" />
              </intent-filter>
          </service>
 
         <!-- Required -->
         <receiver
             android:name= "cn.jpush.android.service.PushReceiver"
             android:enabled= "true"  >
           <intent-filter android:priority= "1000" >
                 <action android:name= "cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY"  />
                 <category android:name= "您应用的包名" />
             </intent-filter>
             <intent-filter>
                 <action android:name= "android.intent.action.USER_PRESENT"  />
                 <action android:name= "android.net.conn.CONNECTIVITY_CHANGE"  />
             </intent-filter>
             <!-- Optional -->
             <intent-filter>
                 <action android:name= "android.intent.action.PACKAGE_ADDED"  />
                 <action android:name= "android.intent.action.PACKAGE_REMOVED"  />
                 <data android:scheme= "package"  />
             </intent-filter>
         </receiver>
      <!-- Required SDK核心功能-->
         <activity
             android:name= "cn.jpush.android.ui.PushActivity"
             android:configChanges= "orientation|keyboardHidden"
             android:exported= "false"  >
             <intent-filter>
                 <action android:name= "cn.jpush.android.ui.PushActivity"  />
                 <category android:name= "android.intent.category.DEFAULT"  />
                 <category android:name= "您应用的包名"  />
             </intent-filter>
         </activity>
         <!-- Required SDK核心功能-->
         <service
             android:name= "cn.jpush.android.service.DownloadService"
             android:enabled= "true"
             android:exported= "false"  >
         </service>
         <!-- Required SDK核心功能-->
         <receiver android:name= "cn.jpush.android.service.AlarmReceiver"  />
 
         <!-- User defined. 用户自定义的广播接收器-->
          <receiver
              android:name= "您自己定义的Receiver"
              android:enabled= "true" >
              <intent-filter>
                  <!--Required 用户注册SDK的intent-->
                  <action android:name= "cn.jpush.android.intent.REGISTRATION"  />
                  <action android:name= "cn.jpush.android.intent.UNREGISTRATION"  />
                  <!--Required 用户接收SDK消息的intent-->
                  <action android:name= "cn.jpush.android.intent.MESSAGE_RECEIVED"  />
                  <!--Required 用户接收SDK通知栏信息的intent-->
                  <action android:name= "cn.jpush.android.intent.NOTIFICATION_RECEIVED"  />
                  <!--Required 用户打开自定义通知栏的intent-->
                  <action android:name= "cn.jpush.android.intent.NOTIFICATION_OPENED"  />
                  <!--Optional 用户接受Rich Push Javascript 回调函数的intent-->
                  <action android:name= "cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK"  />
                  <!-- 接收网络变化 连接/断开 since  1.6 . 3  -->
                  <action android:name= "cn.jpush.android.intent.CONNECTION"  />
                  <category android:name= "您应用的包名"  />
              </intent-filter>
          </receiver>
 
         <!-- Required. For publish channel feature -->
         <!-- JPUSH_CHANNEL 是为了方便开发者统计APK分发渠道。-->
         <!-- 例如: -->
         <!-- 发到 Google Play 的APK可以设置为 google-play; -->
         <!-- 发到其他市场的 APK 可以设置为 xxx-market。 -->
         <!-- 目前这个渠道统计功能的报表还未开放。-->
         <meta-data android:name= "JPUSH_CHANNEL"  android:value= "developer-default" />
         <!-- Required. AppKey copied from Portal -->
         <meta-data android:name= "JPUSH_APPKEY"  android:value= "Your AppKey" />
     </application>
</manifest>

  4,在自定义的application中初始化JPush

1
2
3
4
5
6
7
8
public  class  ExampleApplication  extends  Application {
@Override
         public  void  onCreate() {
             super .onCreate();
     JPushInterface.setDebugMode( true );
     JPushInterface.init( this );
     }
}

 这样就基本上都ok了,但是想要接受自定义的消息的时候需要写一个广播接受者,并注册就ok

自定义的BroadcastREceiver    MyJPushBroadcastReceiver.java 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package  com.example.jpush;
 
import  android.content.BroadcastReceiver;
import  android.content.Context;
import  android.content.Intent;
import  android.os.Bundle;
import  android.util.Log;
import  cn.jpush.android.api.JPushInterface;
 
public  class  MyJPushBroadcastReceiver  extends  BroadcastReceiver {
 
     @Override
     public  void  onReceive(Context context, Intent intent) {
         Bundle bundle = intent.getExtras();
         if  (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
         } else  if  (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
             System.out.println( "收到了自定义消息。消息内容是:"  + bundle.getString(JPushInterface.EXTRA_MESSAGE));
             // 自定义消息不会展示在通知栏,完全要开发者写代码去处理
         else  if  (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
             System.out.println( "收到了通知" );
             // 在这里可以做些统计,或者做些其他工作
         else  if  (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
             System.out.println( "用户点击打开了通知" );
             // 在这里可以自己写代码去定义用户点击后的行为
             Intent i =  new  Intent(context, MainActivity. class );   //自定义打开的界面
             i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             context.startActivity(i);
         else  {
   }
 
     }
 
}

  

这样就可以推送我们自己App的消息和通知了

本文转载自:http://www.cnblogs.com/wjtaigwh/p/4989727.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值