开发指南 - 移动推送 - 阿里云https://help.aliyun.com/document_detail/52906.html
1.在repositories{}代码段中配置maven仓库地址。
allprojects {
repositories {
jcenter()
maven {
url 'http://maven.aliyun.com/nexus/content/repositories/releases/'
}
// 配置HMS Core SDK的Maven仓地址。
maven {
url 'https://developer.huawei.com/repo/'
}
}
}
2.在android{}代码段中配置应用包名和NDK。
android {
......
defaultConfig {
applicationId "com.xxx.xxx" //包名
......
ndk {
//选择要添加的对应cpu类型的.so库。此处仅为示意,推送支持所有主流类型,请根据设备硬件选择
abiFilters 'armeabi', 'x86'
}
......
}
......
}
为了兼容Android 12建议去掉ndk{}这段代码
3.如果在添加以上abFilter配置后Android Studio出现以下提示:
NDK integration is deprecated in the current plugin. Consider trying the new experimental plugin.
在Project根目录的gradle.properties文件中添加:
android.useDeprecatedNdk=true
4.在dependencies{}代码段中添加SDK依赖。
implementation 'com.aliyun.ams:alicloud-android-push:3.7.4'
//华为依赖
implementation 'com.aliyun.ams:alicloud-android-third-push-huawei:3.7.4'
//小米依赖
implementation 'com.aliyun.ams:alicloud-android-third-push-xiaomi:3.7.4'
//OPPO依赖
implementation 'com.aliyun.ams:alicloud-android-third-push-oppo:3.7.4'
//vivo依赖
implementation 'com.aliyun.ams:alicloud-android-third-push-vivo:3.7.4'
//魅族依赖
implementation 'com.aliyun.ams:alicloud-android-third-push-meizu:3.7.4'
//谷歌依赖
implementation 'com.aliyun.ams:alicloud-android-third-push-fcm:3.7.4'
5.在AndroidManifest文件中设置AppKey、AppSecret:
6.权限获取
7.消息接收Receiver配置
import android.content.Context;
import android.util.Log;
import com.alibaba.sdk.android.push.MessageReceiver;
import com.alibaba.sdk.android.push.notification.CPushMessage;
import java.util.Map;
public class MyMessageReceiver extends MessageReceiver {
// 消息接收部分的LOG_TAG
public static final String REC_TAG = "receiver";
@Override
public void onNotification(Context context, String title, String summary, Map extraMap) {
// TODO处理推送通知
Log.e("MyMessageReceiver", "Receive notification, title: " + title + ", summary: " + summary + ", extraMap: " + extraMap);
}
@Override
public void onMessage(Context context, CPushMessage cPushMessage) {
Log.e("MyMessageReceiver", "onMessage, messageId: " + cPushMessage.getMessageId() + ", title: " + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent());
}
@Override
public void onNotificationOpened(Context context, String title, String summary, String extraMap) {
Log.e("MyMessageReceiver", "onNotificationOpened, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
}
@Override
protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {
Log.e("MyMessageReceiver", "onNotificationClickedWithNoAction, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
}
@Override
protected void onNotificationReceivedInApp(Context context, String title, String summary, Map extraMap, int openType, String openActivity, String openUrl) {
Log.e("MyMessageReceiver", "onNotificationReceivedInApp, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap + ", openType:" + openType + ", openActivity:" + openActivity + ", openUrl:" + openUrl);
}
@Override
protected void onNotificationRemoved(Context context, String messageId) {
Log.e("MyMessageReceiver", "onNotificationRemoved");
}
}
8.将该receiver添加到AndroidManifest.xml文件中:
9.处理Android 8+以上设备不显示通知栏问题(请放置在Application文件中,同时请注意服务器端也需要配置,请参考下面链接)
/**
* 阿里云推送兼容Android8
*/
private void aliAndroid8() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 通知渠道的id。
String id = "1";
// 用户可以看到的通知渠道的名字。
CharSequence name = "notification channel";
// 用户可以看到的通知渠道的描述。
String description = "notification description";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
// 配置通知渠道的属性。
mChannel.setDescription(description);
// 设置通知出现时的闪灯(如果Android设备支持的话)。
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
// 设置通知出现时的震动(如果Android设备支持的话)。
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
// 最后在notificationmanager中创建该通知渠道。
mNotificationManager.createNotificationChannel(mChannel);
}
}
Android 8.0以上设备接收不到推送通知https://help.aliyun.com/document_detail/67398.html
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)