首先极光推送分为自动推送和手动推送
接下来的流程皆是手动极光推送的流程
1.自己打开网页极光推送登录之后创建应用
2.创建完成之后会出现自己的应用信息(可以先不用去管),点击下面推送设置---去完成推送设置
3.这个时候出现的页面是让自己设置应用包名,设置包名的前提是和自己创建的项目包名要保持完全一致(Android视图,不用管IOS)
4.当保存完包名以后点击右上角有一个——查看集成指南(需要我们点进去)
5.这个时候是最重要的时候,在 module 的 gradle 中添加依赖和 AndroidManifest 的替换变量。
android {......defaultConfig {applicationId " //JPush 上注册的包名.......ndk {//选择要添加的对应 cpu 类型的 .so 库。abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'// 还可以添加 'x86', 'x86_64', 'mips', 'mips64'}manifestPlaceholders = [JPUSH_PKGNAME : applicationId,JPUSH_APPKEY : "你的 Appkey ", //JPush 上注册的包名对应的 Appkey.JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.]......}......
}dependencies {......implementation 'cn.jiguang.sdk:jpush:3.1.6' // 此处以JPush 3.1.6 版本为例。implementation 'cn.jiguang.sdk:jcore:1.2.5' // 此处以JCore 1.2.5 版本为例。......
}
6.在自己清单文件中复制以下权限(注意千万不要重复!)
<!-- Required --><uses-permission android:name=ample.zk3_demo03.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.MOUNT_UNMOUNT_FILESYSTEMS"tools:ignore="ProtectedPermissions" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.WRITE_SETTINGS"tools:ignore="ProtectedPermissions" /><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /><!-- Optional. Required for location feature --><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" /><uses-permission android:name="android.permission.VIBRATE" />
7.在清单文件的application中放入以下代码(以下代码“您应用的包名”需要改成自己项目的包名,后面带英文的切记不要删)
(下面有会有两个“自己定义的Receiver”,(下面会提示到两个类,请耐心看完)
<!-- Required SDK 核心功能--><!-- 可配置 android:process 参数将 PushService 放在其他进程中 --><serviceandroid: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 3.0.9 Required SDK 核心功能--><providerandroid:authorities="您应用的包名.DataProvider"android:name="cn.jpush.android.service.DataProvider"android:exported="false"<!-- 原码为true,自己要改成false -->/><!-- since 1.8.0 option 可选项。用于同一设备中不同应用的 JPush 服务相互拉起的功能。 --><!-- 若不启用该功能可删除该组件,或把 enabled 设置成 false ;拉起服务被关闭,App 不会通过拉起服务拉起其他的 App,也不会被其他 App 拉起。 --><serviceandroid: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><!-- since 3.1.0 Required SDK 核心功能--><providerandroid:authorities="您应用的包名.DownloadProvider"android:name="cn.jpush.android.service.DownloadProvider"android:exported="true"/><!-- Required SDK 核心功能--><receiverandroid: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=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 核心功能--><activityandroid: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="您应用的包名" /></intent-filter></activity><!-- SDK 核心功能--><activityandroid:name="cn.jpush.android.ui.PopWinActivity"android:configChanges="orientation|keyboardHidden"android:exported="false"android:theme="@style/MyDialogStyle"><intent-filter><category android:name="android.intent.category.DEFAULT" /><category android:name="您应用的包名" /></intent-filter></activity><!-- Required SDK 核心功能--><serviceandroid:name="cn.jpush.android.service.DownloadService"android:enabled="true"android:exported="false" ></service><!-- Required SDK 核心功能--><receiver android:name="cn.jpush.android.service.AlarmReceiver" /><!-- Required since 3.0.7 --><!-- 新的 tag/alias 接口结果返回需要开发者配置一个自定的广播 --><!-- 该广播需要继承 JPush 提供的 JPushMessageReceiver 类, 并如下新增一个 Intent-Filter --><receiverandroid:name="自定义 Receiver"android:enabled="true" android:exported="false" ><intent-filter><action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" /><category android:name="您应用的包名" /></intent-filter></receiver><!-- User defined. 用户自定义的广播接收器--><receiverandroid:name="您自己定义的 Receiver"android:enabled="true"android:exported="false"><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" /><!-- 接收网络变化 连接/断开 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="您应用的 Appkey"/>
8.则在 Project 根目录的 gradle.properties 文件中添加:(报错可以先不用去管,待会创建两个类就ok,参见以下内容)
android.useDeprecatedNdk=true
9.在自己的Application添加以下内容
public class ExampleApplication extends Application {
@Overridepublic void onCreate() {Create();JPushInterface.setDebugMode(true);JPushInterface.init(this);}
}
10.创建的两个类,参考一下内容
创建第一个类
ample.iver;t.BroadcastReceiver;
t.Context;
t.Intent;
import android.os.Bundle;
TextUtils;
import android.util.Log;ample.zk3_demo03.MainActivity;import org.json.JSONException;
import org.json.JSONObject;import java.util.Iterator;import cn.jpush.android.api.JPushInterface;/*** 自定义接收器** 如果不定义这个 Receiver,则:* 1) 默认用户会打开主界面* 2) 接收不到自定义消息*/
public class IReceiver extends BroadcastReceiver {private static final String TAG = "JIGUANG-Example";@Overridepublic void onReceive(Context context, Intent intent) {try {Bundle bundle = Extras();Log.d(TAG, "[MyReceiver] onReceive - " + Action() + ", extras: " + printBundle(bundle));if (JPushInterface.ACTION_REGISTRATION_ID.Action())) {String regId = String(JPushInterface.EXTRA_REGISTRATION_ID);Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);//send the Registration Id to } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.Action())) {Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + String(JPushInterface.EXTRA_MESSAGE));} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.Action())) {Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");int notifactionId = Int(JPushInterface.EXTRA_NOTIFICATION_ID);Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.Action())) {Log.d(TAG, "[MyReceiver] 用户点击打开了通知");//打开自定义的Activity//得到推送后的消息跳转的ActivityIntent i = new Intent(context, MainActivity.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.Action())) {Log.d(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + String(JPushInterface.EXTRA_EXTRA));//在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity, 打开一个网页等..} else if(JPushInterface.ACTION_CONNECTION_CHANGE.Action())) {boolean connected = BooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);Log.w(TAG, "[MyReceiver]" + Action() +" connected state change to "+connected);} else {Log.d(TAG, "[MyReceiver] Unhandled intent - " + Action());}} 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:" + Int(key));}else if(key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)){sb.append("nkey:" + key + ", value:" + Boolean(key));} else if (key.equals(JPushInterface.EXTRA_EXTRA)) {if (TextUtils.String(JPushInterface.EXTRA_EXTRA))) {Log.i(TAG, "This message has no Extra data");continue;}try {JSONObject json = new String(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) {Log.e(TAG, "Get message extra JSON error!");}} else {sb.append("nkey:" + key + ", value:" + (key));}}String();}}
创建的第二个类
t.BroadcastReceiver;
t.Context;
t.Intent;public class MReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {}
}
本文发布于:2024-02-01 22:23:36,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170679741639816.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |