美文网首页推送
极光推送——自动集成(记录自己遇到的坑)

极光推送——自动集成(记录自己遇到的坑)

作者: 感同身受_ | 来源:发表于2019-08-25 21:48 被阅读2次

    按照官方文档的自动集成步骤:

    一、接受通知:

    1. 确认 android studio 的 Project 根目录的主 gradle 中配置了 jcenter 支持。(新建 project 默认配置就支持)
    buildscript {
        repositories {
            jcenter()
        }
        ......
    }
    
    allprojects {
        repositories {
            jcenter()
        }
    }
    
    1. 在 module 的 gradle 中添加依赖和 AndroidManifest 的替换变量。
    android {
        ......
        defaultConfig {
            applicationId "com.xxx.xxx" //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 {
        ......
    
        compile 'cn.jiguang.sdk:jpush:3.1.1'  // 此处以JPush 3.1.1 版本为例。
        compile 'cn.jiguang.sdk:jcore:1.1.9'  // 此处以JCore1.1.9 版本为例。
        ......
    }
    

    【注】A:applicationId "com.xxx.xxx" //JPush 上注册的包名.这个基本是Android Studio自动帮你生成的,一般不用管
    B: JPUSH_APPKEY : "你的 Appkey ", //JPush 上注册的包名对应的 Appkey.这里填上极光推送中注册的Appkey就行,其他不用管
    C:compile 'cn.jiguang.sdk:jpush:3.3.4' // 此处以JPush 3.3.4 版本为例。Android Studio在2019年开始,使用implementation替代 compile
    D:官方给出的例子是这个

    dependencies {
        ......
    
        compile 'cn.jiguang.sdk:jpush:3.3.4'  // 此处以JPush 3.3.4 版本为例。
        compile 'cn.jiguang.sdk:jcore:2.1.2'  // 此处以JCore 2.1.2 版本为例。
        ......
    }
    

    但是我使用这个没怎么成功(还没解决)

    1. 最后,在MainActivity中添加两句话JPushInterface.setDebugMode(true); JPushInterface.init(this);就能成功:
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            JPushInterface.setDebugMode(true);
            JPushInterface.init(this);
    //        JPushInterface.getRegistrationID(this);//获得注册了极光推送设备的ID
        }
    }
    
    【注】如果晚上上面三个步骤,你的极光推送服务后台仍然提示
    错误:
    没有满足条件的推送目标
    
    如果是群发:则此应用还没有一个客户端用户注册。请检查 SDK 集成是否正常。
    如果是推送给某别名或者标签:则此别名或者标签还没有在任何客户端SDK提交设置成功。
    如果是根据 Registration ID 推送:则此 Registration ID 不存在。
    

    我之前也是遇到这个问题,反复做了几个demo,最后发现,只是因为极光推送的“预估人数”存在延迟,一般10多分钟后,才能从“预估人数0”变到“预估人数1”,
    当然,这只是我遇到的坑,如果着都不能解决,建议仔细阅读官方文档检查一下,希望能帮助到你
    官方文档:[http://docs.jiguang.cn/jpush/client/Android/android_debug_guide/
    ]
    【注】一些真机测试时,可能会收不到通知,这就必须自己去手动打开栏目通知:设置->状态栏与通知

    二、接受消息:

    1. 先在Mainfest中添加
    <receiver
        android:name="Your Receiver"
        android:enabled="true"
        android:exported="false">
        <intent-filter>
            <action android:name="cn.jpush.android.intent.REGISTRATION" />
            <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" />
            <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" />
            <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />
            <action android:name="cn.jpush.android.intent.NOTIFICATION_CLICK_ACTION" />
            <action android:name="cn.jpush.android.intent.CONNECTION" />
            <category android:name="You package Name" />
        </intent-filter>
    </receiver>
    

    包名记得替换

    1. 新建一个类MyReceiver继承BroadcastReceiver
    public class MyReceiver extends BroadcastReceiver {
        private static final String TAG = "MyReceiver";
    
        private NotificationManager nm;
    
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();
            Log.d(TAG, "onReceive - " + intent.getAction());
    
            if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
                String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
                Log.d(TAG, "[MyReceiver] 接收 Registration Id : " + regId);
            }else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
                Log.d(TAG, "收到了自定义消息。消息内容是:" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
                // 自定义消息不会展示在通知栏,完全要开发者写代码去处理
            } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
                Log.d(TAG, "收到了通知");
                // 在这里可以做些统计,或者做些其他工作
            } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
                Log.d(TAG, "用户点击打开了通知");
                // 在这里可以自己写代码去定义用户点击后的行为
                Intent i = new Intent(context, MainActivity.class);  //自定义打开的界面
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(i);
            } else {
                Log.d(TAG, "Unhandled intent - " + intent.getAction());
            }
        }
    }
    

    这两部操作后,就能在logcat上查看你后台发送的消息了
    具体参考官方文档[http://docs.jiguang.cn/jpush/client/Android/android_api/#receiver
    ]

    相关文章

      网友评论

        本文标题:极光推送——自动集成(记录自己遇到的坑)

        本文链接:https://www.haomeiwen.com/subject/bmwkectx.html