應(yīng)用場景
1.app在前臺狀態(tài)時,在app內(nèi)推送
2.app在后臺狀態(tài)時,在消息欄推送
集成Jpush
極光推送集成Android 看看文檔,先把sdk集成到項目內(nèi)
邏輯分析
- 我們需要自定義Receiver 去接受jpush發(fā)送過來的消息
- 在接受到的消息中判斷app在前臺狀態(tài)還是后臺狀態(tài)
- 根據(jù)前后臺狀態(tài)做出對應(yīng)的業(yè)務(wù)需求
@Override
public void onReceive(Context context, Intent intent) {
try {
Bundle bundle = intent.getExtras();
MyLogUtils.INSTANCE.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));
//判斷當(dāng)前app的狀態(tài)是(前臺,activity可見為前臺)還是(后臺,activity不可見為后臺)
if (AppUtils.isAppOnForeground(context) &&MainApplication.getInstance().getForeground()) {
//前臺狀態(tài)
JPushInterface.clearAllNotifications(context);
MyLogUtils.INSTANCE.d(TAG,"在前臺。。。。");
if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
MyLogUtils.INSTANCE.d(TAG, "[MyReceiver] 接收到推送下來的通知");
int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
MyLogUtils.INSTANCE.d(TAG, "[MyReceiver] 接收到推送下來的通知的ID: " + notifactionId);
Activity activity = ApplicationUtils.currentActivity();
shouDialog(activity)
} else {
//后臺狀態(tài)
MyLogUtils.INSTANCE.d(TAG,"在后臺。。。。");
if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
MyLogUtils.INSTANCE.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
//send the Registration Id to your server...
} else {
if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
MyLogUtils.INSTANCE.d(TAG, "[MyReceiver] 接收到推送下來的自定義消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
processCustomMessage(context, bundle);
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
MyLogUtils.INSTANCE.d(TAG, "[MyReceiver] 接收到推送下來的通知");
int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
MyLogUtils.INSTANCE.d(TAG, "[MyReceiver] 接收到推送下來的通知的ID: " + notifactionId);
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
MyLogUtils.INSTANCE.d(TAG, "[MyReceiver] 用戶點擊打開了通知");
//打開自定義的Activity
Intent i = new Intent(context, TaskDetailActivity.class);
i.putExtras(bundle);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(i);
} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
MyLogUtils.INSTANCE.d(TAG, "[MyReceiver] 用戶收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
//在這里根據(jù) JPushInterface.EXTRA_EXTRA 的內(nèi)容處理代碼,比如打開新的Activity, 打開一個網(wǎng)頁等..
} else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
MyLogUtils.INSTANCE.d(TAG, "[MyReceiver]" + intent.getAction() + " connected state change to " + connected);
} else {
MyLogUtils.INSTANCE.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
}
}
}
} catch (Exception e) {
}
設(shè)置消息欄的屬性
BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(this);
builder.statusBarDrawable = R.drawable.jpush_notification_icon;
builder.notificationFlags = Notification.FLAG_AUTO_CANCEL; //設(shè)置為點擊后自動消失
builder.notificationDefaults = Notification.DEFAULT_SOUND; //設(shè)置為鈴聲( Notification.DEFAULT_SOUND)或者震動( Notification.DEFAULT_VIBRATE)
JPushInterface.setPushNotificationBuilder(100, builder);//100 這個屬性看文檔
這里有個細節(jié),當(dāng)你設(shè)置了這段代碼,你有可能發(fā)現(xiàn)消息欄上的logo沒有改變,這個時候你重啟下手機就好了
結(jié)語
記一次需求,方便以后萬一開發(fā)相同的需求用到,也說不定哪個小伙伴有類似的需求可以參考一下呢,好記性不如爛筆頭,下次見咯。