极光推送,怎么使用用广播接收

此处省略,配置依赖,和权限等问题(查看官网),只展示中间部分


清单文件

             极光推送
       <!-- Required SDK 核心功能-->
       <!-- 可配置android:process参数将PushService放在其他进程中 -->
       <service
           android: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 核心功能-->
       <provider
           android:authorities="你的包名.DataProvider"
           android:name="cn.jpush.android.service.DataProvider"
           android:exported="true"
           tools:replace="android:exported"//配置官方文档的时候这里容易报错,加一行这个
           />
       <!-- since 3.1.0 Required SDK 核心功能-->
       <provider
           android:authorities="你的包名.DownloadProvider"
           android:name="cn.jpush.android.service.DownloadProvider"
           android:exported="true"
           />

       <!-- Required SDK核心功能-->
       <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="你的包名"/>
           </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>

       <!-- Required SDK核心功能-->
       <activity
           android: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>
       <!-- Required SDK核心功能-->
       <service
           android:name="cn.jpush.android.service.DownloadService"
           android:enabled="true"
           android:exported="false" >
       </service>

       <!-- Required SDK核心功能-->
       <receiver android:name="cn.jpush.android.service.AlarmReceiver" />
       //自己定义的广播,用于接收推送下来的消息,并进行处理的
       <receiver
           android:name=".other.MyReceiver"
           android:enabled="true" >
           <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>


       <meta-data android:name="JPUSH_CHANNEL" android:value="developer-default"/>
       <!-- Required. AppKey copied from Portal -->
       <meta-data android:name="JPUSH_APPKEY" android:value="申请的appKey"/>

自己定义的广播

public class MyReceiver extends BroadcastReceiver {
    private static final String TAG = "JPush";
    private Intent i;
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));

        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
            //send the Registration Id to your server...

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            processCustomMessage(context, bundle);

        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");
            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);

        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 用户点击打开了通知");
            Log.d(TAG, "[MyReceiver] 用户点击打开了通知内容"+bundle.getString(JPushInterface.EXTRA_ALERT));
            Log.d(TAG, "[MyReceiver] 用户点击打开了通知json"+bundle.getString(JPushInterface.EXTRA_EXTRA));

        } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
            //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity, 打开一个网页等..

        } else if(JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
            boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
            Log.w(TAG, "[MyReceiver]" + intent.getAction() +" connected state change to "+connected);
        } else {
            Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
        }
    }

    // 打印所有的 intent extra 数据
    private static String printBundle(Bundle bundle) {
        // ...//省略了
        return null;
    }

    //send msg to MainActivity
    private void processCustomMessage(Context context, Bundle bundle) {
        //  ...//省略了
    }
}
最后运行一下,看一下广播里面的日志信息是否打印,如果有打印证明配置成功了


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值