极光推送注册、接收、点击打开推送通知
来源:互联网 发布:淘宝家具店排名 编辑:程序博客网 时间:2024/06/03 12:30
在项目中用到了极光推送,所以把项目中做过的分享一下:
1.下载极光推送所需要的包,如:
2.在AndroidManifest中配置权限和类,如下:
<!-- Required --> <permission android:name="您应用的包名.permission.JPUSH_MESSAGE" android:protectionLevel="signature" /> <!-- Required --> <uses-permission android:name="您应用的包名.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" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <!-- Optional. Required for location feature --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <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" /> <uses-permission android:name="android.permission.GET_TASKS" /> <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" /> <!--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"/>
3.在Application里面注册,也就是初始化
JPushInterface.setDebugMode(true); // 设置开启日志,发布时请关闭日志JPushInterface.init(context.getApplicationContext()); // 初始化 JPush
4.自定义的广播接受者的代码如下,这里加的代码都是我自己加的,写法不一,这里可以接受到推送的消息,点击跳转自定义的界面:
private static final String TAG = "JPush";@Overridepublic void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle)); if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) { String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID); Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId); } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) { Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE)); } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) { Log.d(TAG, "[MyReceiver] 接收到推送下来的通知"); int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID); Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId); } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) { Log.d(TAG, "[MyReceiver] 用户点击打开了通知"); //打开自定义的Activity chooseMyActivity(context, bundle); } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) { Log.d(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA)); //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity, 打开一个网页等.. } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) { boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false); Log.w(TAG, "[MyReceiver]" + intent.getAction() + " connected state change to " + connected); } else { Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction()); }}// 打印所有的 intent extra 数据private static String printBundle(Bundle bundle) { StringBuilder sb = new StringBuilder(); for (String key : bundle.keySet()) { if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) { sb.append("\nkey:" + key + ", value:" + bundle.getInt(key)); } else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) { sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key)); } else { sb.append("\nkey:" + key + ", value:" + bundle.getString(key)); } } return sb.toString();}// send msg to MyActivityprivate void chooseMyActivity(Context context, Bundle bundle) { // 打开自定义的Activity try { JSONObject jo = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA)); int code = Integer.parseInt(jo.getString("code")); bundle.clear(); switch (code) { case CommonData.JPUSH_URL://100:url, bundle.putString("Code", CommonData.JPUSH_URL + ""); bundle.putString("url", jo.getString("param")); startMyActivity(context, bundle, WebViewActivity.class); break; case CommonData.JPUSH_TXT://101:纯文本 bundle.putString("Code", CommonData.JPUSH_TXT + ""); bundle.putString("param", jo.getString("param")); startMyActivity(context, bundle, TxtActivity.class); break; default: break; } } } catch (JSONException e) { e.printStackTrace(); }}private void startMyActivity(Context context, Bundle bundle, Class<?> name) { Intent i = new Intent(context, name); i.putExtras(bundle); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i);}
以上就是极光推送的内容了,但是要实现自定义消息的推送,这就是你和你们后台的事了,如别名、自定义通知栏样式等等。这里就不一一写出了。
0 0
- 极光推送注册、接收、点击打开推送通知
- iOS 极光推送接收通知
- 信鸽推送接收通知点击打开相应Activity
- 极光推送-点击通知栏跳到指定页面
- 极光推送怎么点击通知跳转页面?
- 极光推送 推送处理总结 点击通知跳转
- 极光推送注册代码
- iOS10注册极光推送
- iOS10注册极光推送
- iOS10 注册极光推送
- iOS 注册极光推送
- 极光推送通知和消息
- 极光推送和通知栏
- 极光推送接收不到消息
- iOS10 注册极光推送(干货)
- iOS10 注册极光推送(干货)
- iOS10注册极光推送(干货)
- iOS10 注册极光推送(干货)
- lightoj 1021 从当前转移到其他状态的状压dp
- Java基础——实现UDP发送数据和接收数据
- 数据库实战(四)数据库主从复制
- R语言内存管理
- 常用less
- 极光推送注册、接收、点击打开推送通知
- Python学习笔记
- trie树字典树的插入和删除
- 第一行代码笔记,第六章-----详解数据存储
- 第十课 使用java开发spark 实战
- android 自定义View 的详细介绍
- 读取plist文件中的数据
- Ethercalc中的Post commands
- JAVA volatile 关键字详解