Android集成新版极光推送服务(4.0.6版)
关于
虽然极光推送官方文档和对应的demo里面都有介绍使用,但是还是感觉云里雾里,可能会有人和我一样要花个大半天时间才能集成好,这也是本篇要记录的原因,以备后面其余项目使用。
使用第一步
首先去官网新建一个应用,获取对应的一个appkey。然后点击消息推送进行集成:
输入我们要集成的项目的包名(build里面的applicationId),下面的其他推送渠道我没去勾选,有需要的童鞋可以去配置。
然后我们选择下载SDK,里面会有我们需要的配置的jar包和.so库文件:
复制到我们的项目中:
并且将上面两个jar包添加到libray中,添加ndk到build中:
defaultConfig {
applicationId "com.tobey.newstest"
manifestPlaceholders = [
JPUSH_PKGNAME: applicationId,
JPUSH_APPKEY : "d292495db1604dd08bbexxxx", //JPush 上注册的包名对应的 Appkey.
JPUSH_CHANNEL: "developer-default", //暂时填写默认值即可.
]
ndk {
//cpu类型对应的.so库。
abiFilters 'arm64', 'armeabi-v7a'
}
}
修改androidmanifest.xml配置文件
添加权限:
<permission android:name="${applicationId}.permission.JPUSH_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission android:name="${applicationId}.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.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
在application内部添加如下:
<service
android:name="cn.jpush.android.service.PushService"
android:enabled="true"
android:exported="false"
android:process=":pushcore">
<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>
<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="${applicationId}"/>
</intent-filter>
</service>
<service android:name=".jiguanPush.PushService"
android:enabled="true"
android:exported="false"
android:process=":pushcore">
<intent-filter>
<action android:name="cn.jiguang.user.service.action" />
</intent-filter>
</service>
<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="${applicationId}"/>
</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>
<!-- since 3.3.0 Required SDK核心功能 -->
<activity
android:name="cn.jpush.android.service.JNotifyActivity"
android:exported="true"
android:taskAffinity="jpush.custom"
android:theme="@android:style/Theme.Translucent.NoTitleBar" >
<intent-filter>
<action android:name="cn.jpush.android.intent.JNotifyActivity" />
<category android:name="${applicationId}" />
</intent-filter>
</activity>
<!-- 3.3.0开始所有事件将通过该类回调 -->
<!-- 该广播需要继承 JPush 提供的 JPushMessageReceiver 类, 并如下新增一个 Intent-Filter -->
<receiver
android:name=".jiguanPush.PushMessageReceiver"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
<receiver android:name="cn.jpush.android.service.AlarmReceiver" />
<meta-data
android:name="JPUSH_CHANNEL"
android:value="developer-default" />
<!-- 添加appkey-->
<meta-data
android:name="JPUSH_APPKEY"
android:value="d292495db1604dd08bxxxx" />
<!-- since 3.0.9 Required SDK 核心功能-->
<provider
android:authorities="${applicationId}.DataProvider"
android:name="cn.jpush.android.service.DataProvider"
android:exported="false"
android:process=":pushcore"
/>
初始化
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
JPushInterface.setDebugMode(true);
JPushInterface.init(this);
}
}
然后在对应权限都获取了以后,在极光推送平台上发送一条推送:
然后在手机上收到通知:
如果提示没有没有满足条件的推送目标或推送目标超过 255 天不活跃,被排除在推送目标之外,说明没配置好,严格检查build文件、用户权限、包名、appkey、配置文件是否配置好。
到此本篇结束,有问题欢迎批评指正!