先注册账户:
然后点击开发者服务:点击打开链接
创建应用:
随便起个名字,但是最好和你的应用名字一样
然后点击下一步推送设置
把你的工程应用名字输入:
应用包名就是build.gradle文件里的applicationID 名字
完成之后点击下载Demo
Demo下载完成之后解压 ,压缩包
将libs文件夹里的工具jar包全部复制到你的项目中,记得编译
将文件中的jar包导入工程中的libs文件夹 并引用,
在将res文件夹直接复制到项目中的src文件夹下的main文件夹里,
它会直接补齐你工程中缺少的部分,所以不用害怕它会替换掉你的原文件
使用 androID studio 的开发者,如果使用 jnilibs 文件夹导入 so 文件,则仅需将所有 cpu 类型的文件夹拷进去;如果将 so 文件添加在 module的libs 文件夹下,注意在 module 的 gradle 配置中添加一下配置:
sourceSets { main { jnilibs.srcDirs = ['libs'] }}注意点,还有 jnilibs 空文件夹不要忘
MyApp 类(记得在清单文件中添加name)public class MyApp extends Application { public static String registrationID ;//获取 极光推送的设备唯一性标识 RegistrationID @OverrIDe public voID onCreate() { super.onCreate(); //极光推送 JPushInterface.setDeBUGMode(true); // 设置开启日志,发布时请关闭日志 JPushInterface.init(this); // 初始化 JPush registrationID = JPushInterface.getRegistrationID(this);//获取 极光推送的设备唯一性标识 RegistrationID Log.e("111111registrationID", "run:--------->:" + registrationID ); }}
MyReceiver
TestActivity
Logger
MyReceiver |
package com.sgy.sgy_jpush; import androID.content.broadcastReceiver;import androID.content.Context;import androID.content.Intent;import androID.os.Bundle;import androID.text.TextUtils;import org.Json.JsONException;import org.Json.JsONObject;import java.util.Iterator;import cn.jpush.androID.API.JPushInterface; /** * 自定义接收器 * * 如果不定义这个 Receiver,则: * 1) 默认用户会打开主界面 * 2) 接收不到自定义消息 */public class MyReceiver extends broadcastReceiver { private static final String TAG = "JIGUANG-Example"; @OverrIDe public voID onReceive(Context context, Intent intent) { try { Bundle bundle = intent.getExtras(); Logger.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); Logger.d(TAG, "[MyReceiver] 接收Registration ID : " + regID); //send the Registration ID to your server... } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) { Logger.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));// processCustomMessage(context, bundle); } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) { Logger.d(TAG, "[MyReceiver] 接收到推送下来的通知"); int notifactionID = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID); Logger.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionID); } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) { Logger.d(TAG, "[MyReceiver] 用户点击打开了通知"); //打开自定义的Activity Intent i = new Intent(context, TestActivity.class); i.putExtras(bundle); //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_top ); context.startActivity(i); } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) { Logger.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); Logger.w(TAG, "[MyReceiver]" + intent.getAction() +" connected state change to "+connected); } else { Logger.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction()); } } catch (Exception e){ } } // 打印所有的 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 if (key.equals(JPushInterface.EXTRA_EXTRA)) { if (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) { Logger.i(TAG, "This message has no Extra data"); continue; } try { JsONObject Json = new JsONObject(bundle.getString(JPushInterface.EXTRA_EXTRA)); Iterator<String> it = Json.keys(); while (it.hasNext()) { String myKey = it.next(); sb.append("\nkey:" + key + ", value: [" + myKey + " - " +Json.optString(myKey) + "]"); } } catch (JsONException e) { Logger.e(TAG, "Get message extra JsON error!"); } } else { sb.append("\nkey:" + key + ", value:" + bundle.get(key)); } } return sb.toString(); } //send msg to MainActivity// private voID processCustomMessage(Context context, Bundle bundle) {// if (MainActivity.isForeground) {// String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);// String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);// Intent msgintent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);// msgintent.putExtra(MainActivity.KEY_MESSAGE, message);// if (!ExampleUtil.isEmpty(extras)) {// try {// JsONObject extraJson = new JsONObject(extras);// if (extraJson.length() > 0) {// msgintent.putExtra(MainActivity.KEY_EXTRAS, extras);// }// } catch (JsONException e) {//// }//// }// LocalbroadcastManager.getInstance(context).sendbroadcast(msgintent);// }// }} |
TestActivity |
package com.sgy.sgy_jpush; import androID.app.Activity;import androID.content.Intent;import androID.os.Bundle;import androID.vIEw.VIEwGroup.LayoutParams;import androID.Widget.TextVIEw;import cn.jpush.androID.API.JPushInterface; public class TestActivity extends Activity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextVIEw tv = new TextVIEw(this); tv.setText("用户自定义打开的Activity"); Intent intent = getIntent(); if (null != intent) { Bundle bundle = getIntent().getExtras(); String Title = null; String content = null; if(bundle!=null){ Title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_Title); content = bundle.getString(JPushInterface.EXTRA_ALERT); } tv.setText("Title : " + Title + " " + "Content : " + content); } addContentVIEw(tv, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); } } |
Logger |
package com.sgy.sgy_jpush; import androID.util.Log; /** * Created by efan on 2017/4/13. */ public class Logger { //设为false关闭日志 private static final boolean LOG_ENABLE = true; public static voID i(String tag, String msg){ if (LOG_ENABLE){ Log.i(tag, msg); } } public static voID v(String tag, String msg){ if (LOG_ENABLE){ Log.v(tag, msg); } } public static voID d(String tag, String msg){ if (LOG_ENABLE){ Log.d(tag, msg); } } public static voID w(String tag, String msg){ if (LOG_ENABLE){ Log.w(tag, msg); } } public static voID e(String tag, String msg){ if (LOG_ENABLE){ Log.e(tag, msg); } }}
|
清单文件:加权限: <!-- required --> <permission androID:name="com.sgy.sgy_jpush.permission.JPUSH_MESSAGE" androID:protectionLevel="signature" /> <!-- required 一些系统要求的权限,如访问网络等--> <uses-permission androID:name="com.sgy.sgy_jpush.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.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.WRITE_SETTINGS" /> <uses-permission androID:name="androID.permission.MOUNT_UNMOUNT_fileSYstemS" /> <uses-permission androID:name="androID.permission.ACCESS_NETWORK_STATE" /> <uses-permission androID:name="androID.permission.ACCESS_WIFI_STATE" /> <!-- Optional for location --> <uses-permission androID:name="androID.permission.VIBRATE" /> <uses-permission androID:name="androID.permission.SYstem_ALERT_WINDOW" /> <!-- 用于开启 deBUG 版本的应用在6.0 系统上 层叠窗口权限 --> <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" />
<!-- For test only 测试状态通知栏,需要打开的Activity --> <activity androID:name="com.sgy.sgy_jpush.TestActivity" androID:exported="false"> <intent-filter> <action androID:name="jpush.testAction" /> <category androID:name="jpush.testcategory" /> </intent-filter> </activity> <!-- Rich push 核心功能 since 2.0.6--> <activity androID:name="cn.jpush.androID.ui.PopWinActivity" androID:theme="@style/MyDialogStyle" androID:exported="false"> </activity> <!-- required SDK核心功能--> <activity androID:name="cn.jpush.androID.ui.PushActivity" androID:configChanges="orIEntation|keyboardHIDden" androID:theme="@androID:style/theme.NoTitlebar" androID:exported="false"> <intent-filter> <action androID:name="cn.jpush.androID.ui.PushActivity" /> <category androID:name="androID.intent.category.DEFAulT" /> <category androID:name="com.sgy.sgy_jpush" /> </intent-filter> </activity> <!-- required SDK 核心功能--> <!-- 可配置androID:process参数将PushService放在其他进程中 --> <service androID:name="cn.jpush.androID.service.PushService" androID:process=":pushcore" 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 3.0.9 required SDK 核心功能--> <provIDer androID:authoritIEs="com.sgy.sgy_jpush.DataProvIDer" androID:name="cn.jpush.androID.service.DataProvIDer" androID:process=":pushcore" androID:exported="false" /> <!-- 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="com.sgy.sgy_jpush" /> </intent-filter> </service> <!-- since 3.1.0 required SDK 核心功能--> <provIDer androID:authoritIEs="com.sgy.sgy_jpush.DownloadProvIDer" androID:name="cn.jpush.androID.service.DownloadProvIDer" androID:exported="true" /> <!-- required SDK核心功能--> <receiver androID:name="cn.jpush.androID.service.PushReceiver" androID:enabled="true" androID:exported="false"> <intent-filter androID:priority="1000"> <action androID:name="cn.jpush.androID.intent.NOTIFICATION_RECEIVED_PROXY" /> <!--required 显示通知栏 --> <category androID:name="com.sgy.sgy_jpush" /> </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核心功能--> <receiver androID:name="cn.jpush.androID.service.AlarmReceiver" androID:exported="false"/> <!-- User defined. For test only 用户自定义的广播接收器--> <receiver androID:name="com.sgy.sgy_jpush.MyReceiver" androID:exported="false" androID:enabled="true"> <intent-filter> <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 用户接收SDK通知栏信息的intent--> <action androID:name="cn.jpush.androID.intent.NOTIFICATION_OPENED" /> <!--required 用户打开自定义通知栏的intent--> <action androID:name="cn.jpush.androID.intent.CONNECTION" /><!-- 接收网络变化 连接/断开 since 1.6.3 --> <category androID:name="com.sgy.sgy_jpush" /> </intent-filter> </receiver> <!-- required . Enable it you can get statistics data with channel --> <Meta-data androID:name="JPUSH_CHANNEL" androID:value="developer-default"/> <Meta-data androID:name="JPUSH_APPKEY" androID:value="da075747e2a374d552993f2a" /> <!-- </>值来自开发者平台取得的AppKey-->
然后运行一下工程:
在回到极光平台:点击打开链接
哈哈哈,然后你就可以收到推送的消息啦,是不是很简单呢!!!
最后在附上完整的AndroIDManifest.xml清单文件:
<?xml version="1.0" enCoding="utf-8"?>
<manifest xmlns:androID="http://schemas.androID.com/apk/res/androID"
package="com.sgy.sgy_jpush">
<!-- required -->
<permission
androID:name="com.sgy.sgy_jpush.permission.JPUSH_MESSAGE"
androID:protectionLevel="signature" />
<!-- required 一些系统要求的权限,如访问网络等-->
<uses-permission androID:name="com.sgy.sgy_jpush.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.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.WRITE_SETTINGS" />
<uses-permission androID:name="androID.permission.MOUNT_UNMOUNT_fileSYstemS" />
<uses-permission androID:name="androID.permission.ACCESS_NETWORK_STATE" />
<uses-permission androID:name="androID.permission.ACCESS_WIFI_STATE" />
<!-- Optional for location -->
<uses-permission androID:name="androID.permission.VIBRATE" />
<uses-permission androID:name="androID.permission.SYstem_ALERT_WINDOW" /> <!-- 用于开启 deBUG 版本的应用在6.0 系统上 层叠窗口权限 -->
<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:name=".MyApp"
androID:allowBackup="true"
androID:icon="@mipmap/ic_launcher"
androID:label="@string/app_name"
androID:roundIcon="@mipmap/ic_launcher_round"
androID:supportsRtl="true"
androID:theme="@style/Apptheme">
<activity androID:name=".MainActivity">
<intent-filter>
<action androID:name="androID.intent.action.MAIN" />
<category androID:name="androID.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- For test only 测试状态通知栏,需要打开的Activity -->
<activity androID:name="com.sgy.sgy_jpush.TestActivity" androID:exported="false">
<intent-filter>
<action androID:name="jpush.testAction" />
<category androID:name="jpush.testcategory" />
</intent-filter>
</activity>
<!-- Rich push 核心功能 since 2.0.6-->
<activity
androID:name="cn.jpush.androID.ui.PopWinActivity"
androID:theme="@style/MyDialogStyle"
androID:exported="false">
</activity>
<!-- required SDK核心功能-->
<activity
androID:name="cn.jpush.androID.ui.PushActivity"
androID:configChanges="orIEntation|keyboardHIDden"
androID:theme="@androID:style/theme.NoTitlebar"
androID:exported="false">
<intent-filter>
<action androID:name="cn.jpush.androID.ui.PushActivity" />
<category androID:name="androID.intent.category.DEFAulT" />
<category androID:name="com.sgy.sgy_jpush" />
</intent-filter>
</activity>
<!-- required SDK 核心功能-->
<!-- 可配置androID:process参数将PushService放在其他进程中 -->
<service
androID:name="cn.jpush.androID.service.PushService"
androID:process=":pushcore"
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 3.0.9 required SDK 核心功能-->
<provIDer
androID:authoritIEs="com.sgy.sgy_jpush.DataProvIDer"
androID:name="cn.jpush.androID.service.DataProvIDer"
androID:process=":pushcore"
androID:exported="false"
/>
<!-- 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="com.sgy.sgy_jpush" />
</intent-filter>
</service>
<!-- since 3.1.0 required SDK 核心功能-->
<provIDer
androID:authoritIEs="com.sgy.sgy_jpush.DownloadProvIDer"
androID:name="cn.jpush.androID.service.DownloadProvIDer"
androID:exported="true"
/>
<!-- required SDK核心功能-->
<receiver
androID:name="cn.jpush.androID.service.PushReceiver"
androID:enabled="true"
androID:exported="false">
<intent-filter androID:priority="1000">
<action androID:name="cn.jpush.androID.intent.NOTIFICATION_RECEIVED_PROXY" /> <!--required 显示通知栏 -->
<category androID:name="com.sgy.sgy_jpush" />
</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核心功能-->
<receiver androID:name="cn.jpush.androID.service.AlarmReceiver" androID:exported="false"/>
<!-- User defined. For test only 用户自定义的广播接收器-->
<receiver
androID:name="com.sgy.sgy_jpush.MyReceiver"
androID:exported="false"
androID:enabled="true">
<intent-filter>
<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 用户接收SDK通知栏信息的intent-->
<action androID:name="cn.jpush.androID.intent.NOTIFICATION_OPENED" /> <!--required 用户打开自定义通知栏的intent-->
<action androID:name="cn.jpush.androID.intent.CONNECTION" /><!-- 接收网络变化 连接/断开 since 1.6.3 -->
<category androID:name="com.sgy.sgy_jpush" />
</intent-filter>
</receiver>
<!-- required . Enable it you can get statistics data with channel -->
<Meta-data androID:name="JPUSH_CHANNEL" androID:value="developer-default"/>
<Meta-data androID:name="JPUSH_APPKEY" androID:value="da075747e2a374d552993f2a" /> <!-- </>值来自开发者平台取得的AppKey-->
</application>
</manifest>
总结
以上是内存溢出为你收集整理的极光推送jpush(简单易懂,分分钟教你搞定)全部内容,希望文章能够帮你解决极光推送jpush(简单易懂,分分钟教你搞定)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)