1,集成极光sdk,申请appkey
2,manifest注册
权限申请参考官方文档
<!-- 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>
<!-- 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="com.shilian.collector" />
</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:exported="false"
android:theme="@android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="cn.jpush.android.ui.PushActivity" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.shilian.collector" />
</intent-filter>
</activity>
<!-- Required SDK核心功能 -->
<service
android:name="cn.jpush.android.service.DownloadService"
android:enabled="true"
android:exported="false" />
<!-- Required SDK核心功能 -->
<receiver android:name="cn.jpush.android.service.AlarmReceiver" />
<!-- User defined. 用户自定义的广播接收器 -->
<receiver
android:name=".receive.MyJPushReceiver"
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="com.shilian.collector" />
</intent-filter>
</receiver>
<!-- Required. For publish channel feature -->
<!-- JPUSH_CHANNEL 是为了方便开发者统计APK分发渠道。 -->
<!-- 例如: -->
<!-- 发到 Google Play 的APK可以设置为 google-play; -->
<!-- 发到其他市场的 APK 可以设置为 xxx-market。 -->
<!-- 目前这个渠道统计功能的报表还未开放。 -->
<meta-data
android:name="JPUSH_CHANNEL"
android:value="developer-default" />
<!-- Required. AppKey copied from Portal -->
<meta-data
android:name="JPUSH_APPKEY"
android:value="8a71726183946338130ee4f2" />
3,开启极光推送
//开启极光推送
JPushInterface.setDebugMode(false);//如果时正式版就改成false
JPushInterface.init(this);
JPushId=JPushInterface.getRegistrationID(this);
//设置别名
JPushInterface.setAliasAndTags(mActivity.getApplicationContext(), ris.getResultMap().getUserid() + "", null, new TagAliasCallback() {
@Override
public void gotResult(int i, String s, Set<String> set) {
switch (i) {
case 0:
Toast.makeText(mActivity,"登录成功",Toast.LENGTH_SHORT).show();
break;
case 6002:
toast("网络不稳定,别名设置失败,请从新登录");
break;
default:
toast("别名设置失败,请从新登录");
break;
}
}
});
4,极光推送广播接收器,可接收自定义消息,但在程序进程别杀死的时候,仅能收到通知,收不到自定义消息
/**
* 极光推送的自定义广播接收者
* Created by Burn on 2016/12/13.
*/
public class MyJPushReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle=intent.getExtras();
if (BuildConfig.DEBUG){
Log.e("tag_receiver","action:"+intent.getAction());
}
if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())){
//自定义消息
String message=bundle.getString(JPushInterface.EXTRA_MESSAGE);
String title=bundle.getString(JPushInterface.EXTRA_TITLE);
if (BuildConfig.DEBUG){
Log.e("tag_receiver","message:"+message);
Log.e("tag_receiver","title:"+title);
}
NotificationManager nm= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification=new Notification();
notification.flags=Notification.FLAG_AUTO_CANCEL;
notification.defaults=Notification.DEFAULT_SOUND;
notification.icon= R.mipmap.icon_launcher;
Intent onClickIntent=new Intent(context, HomeActivity.class).putExtra("clearCount",true);
notification.contentIntent= PendingIntent.getActivity(context,1,onClickIntent,PendingIntent.FLAG_CANCEL_CURRENT);
RemoteViews contentView=new RemoteViews(context.getPackageName(),R.layout.layout_notification);
notification.contentView=contentView;
//notification.contentView.setTextViewText(R.id.textView_title_notification,title);
notification.contentView.setTextViewText(R.id.textView_message_notification,message);
int count=MyApplication.getJPushCount();
notification.contentView.setTextViewText(R.id.textView_num_notification, ++count + "条");
MyApplication.setJPushCount(count);
notification.contentView.setTextViewText(R.id.textView_time_notification, new SimpleDateFormat("hh:mm").format(new Date()));
nm.notify(1, notification);
}
}
}