现在的APP可谓是不可没有消息推送,这样可以提高用户的粘滞性,结合着大数据算法,推送一些关心的内容
消息推送的第三方SDK有很多 极光/个推/小米/华为等,当然也可以公司开发一套消息推送
下面是极光推送集成指南(之前做过很多次,一直没有分享)
setp1: 导入SDK并配置引用,然后引入各个平台的so文件
然后在配置清单文件中加入:
在gradle中的
defaultConfig{
ndk {
abiFilters 'armeabi-v7a', 'x86', 'x86_64', 'armeabi-v7a'//,'arm64-v8a'
}
manifestPlaceholders = [
JPUSH_PKGNAME: applicationId,
JPUSH_APPKEY : "你的ID", //JPush 上注册的包名对应的 Appkey.
JPUSH_CHANNEL: "developer-default", //暂时填写默认值即可.
]
}
这样 配置就算可以了,然后在包下,复制从官网下载的demo,到包里,参考地址:https://docs.jiguang.cn//jpush/client/Android/android_3m/
比如TagAliasOperatorHelper这个工具类 改好以后在全局的Applition中 初始化
JPushInterface.setDebugMode(true);
JPushInterface.init(this);
String registrationID = JPushInterface.getRegistrationID(this);//这个可以传给后台 推送到具体的个人
如上配置好之后:
检查一下有没有通知栏权限
package xxx;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class CheckAndStartNitifycationUtils
{
public static boolean isNotificationEnabled(Context context) {
String CHECK_OP_NO_THROW = "checkOpNoThrow";
String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";
AppOpsManager mAppOps = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
}
ApplicationInfo appInfo = context.getApplicationInfo();
String pkg = context.getApplicationContext().getPackageName();
int uid = appInfo.uid;
Class appOpsClass = null;
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
appOpsClass = Class.forName(AppOpsManager.class.getName());
}
Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE,
String.class);
Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
int value = (Integer) opPostNotificationValue.get(Integer.class);
return ((Integer) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static void startNotifyCationAction(Context context)
{
// boolean enabled = isNotificationEnabled(context);
// if (!enabled) {
/**
* 跳到通知栏设置界面
* @param context
*/
Intent localIntent = new Intent();
//直接跳转到应用通知设置的代码:
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
localIntent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
localIntent.putExtra("app_package", context.getPackageName());
localIntent.putExtra("app_uid", context.getApplicationInfo().uid);
} else if (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
localIntent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
localIntent.addCategory(Intent.CATEGORY_DEFAULT);
localIntent.setData(Uri.parse("package:" + context.getPackageName()));
} else {
//4.4以下没有从app跳转到应用通知设置页面的Action,可考虑跳转到应用详情页面,
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= 9) {
localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
} else if (Build.VERSION.SDK_INT <= 8) {
localIntent.setAction(Intent.ACTION_VIEW);
localIntent.setClassName("com.android.settings", "com.android.setting.InstalledAppDetails");
localIntent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
}
}
context.startActivity(localIntent);
}
// }
}
这样就可以去调试了