轉(zhuǎn)載請(qǐng)注明原作者,如果你覺(jué)得這篇文章對(duì)你有幫助或啟發(fā),可以關(guān)注打賞。
Pushwoosh是境外的一個(gè)提供免費(fèi)推送服務(wù)的公司,Android app當(dāng)然也是基于Google Cloud Messaging 封裝的。
因?yàn)?a target="_blank" rel="nofollow">官方文檔不太直觀,也可能是之前不太了解過(guò)國(guó)外此類第三方服務(wù)的套路,使用過(guò)程也遇到了一些小問(wèn)題,算是采坑了吧!測(cè)試也請(qǐng)使用包含完整Google服務(wù)框架的真機(jī)。
添加依賴
compile 'com.pushwoosh:pushwoosh:+'
compile 'com.android.support:support-v4:23.1.1+'
compile 'com.google.android.gms:play-services-gcm:8.4.0+'
compile 'com.google.android.gms:play-services-location:8.4.0+'
如果不使用Geozones push的話就不需要添加location依賴了。
XML
在AndroidManifest.xml中application節(jié)點(diǎn)下添加:
<meta-data android:name="PW_APPID" android:value="XXXXX-XXXXX" />
<meta-data android:name="PW_PROJECT_ID" android:value="A123456789012" />
PW_APPID是在Pushwoosh創(chuàng)建應(yīng)用的ID
PW_PROJECT_ID是從Google開(kāi)發(fā)者控制臺(tái)設(shè)置GCM拿來(lái)的工程號(hào),
notice: 你需要給這個(gè)工程號(hào)手動(dòng)添加前綴“A”。
NotificationFactory
自定義NotificationFactory繼承DefaultNotificationFactory,實(shí)現(xiàn)自己的通知樣式。
public class CustomContentNotificationFactory extends DefaultNotificationFactory {
@Override
public Notification onGenerateNotification(PushData pushData) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
return super.onGenerateNotification(pushData);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getContext())
.setContentTitle(getContentFromHtml(pushData.getHeader()))
.setContentText(getContentFromHtml(pushData.getMessage()))
.setSmallIcon(R.drawable.ic_notification_small)
.setLargeIcon(BitmapFactory.decodeResource(getContext().getResources(), R.mipmap.ic_launcher))
.setTicker(getContentFromHtml(pushData.getTicker()))
.setWhen(System.currentTimeMillis())
.setPriority(Notification.PRIORITY_HIGH);
final Notification notification = notificationBuilder.build();
addSound(notification, pushData.getSound());
addVibration(notification, pushData.getVibration());
addCancel(notification);
return notification;
}
}
這一步定義了通知的樣式,這樣我們收到推送后就會(huì)顯示自定義的通知樣式及數(shù)據(jù),但一般我們需要在點(diǎn)擊通知后跳轉(zhuǎn)到app指定的頁(yè)面,這里大家就不要想著給通知添加PendingIntent了,當(dāng)然添加7.0的Action是可以的。
PushReceiver
- 自定義PushReceiver繼承BroadcastReceiver,這樣收到推送彈出通知,點(diǎn)擊通知后就會(huì)進(jìn)入PushReceiver的onReceive(),在這里實(shí)現(xiàn)跳轉(zhuǎn)邏輯。
public class PushReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null)
return;
Bundle bundle = PushManagerImpl.preHandlePush(context, intent);
if (bundle == null)
return;
String type = bundle.getString(Constants.TYPE);
String id = bundle.getString(Constants.ID);
String sn = bundle.getString(Constants.SN);
if (TextUtils.isEmpty(type))
return;
switch (type) {
case Constants.PUSH_SCENE:
if (!TextUtils.isEmpty(id) && !TextUtils.equals("0", id))
intent = SceneActivity.newIntent(context, new Scene().setId(Integer.parseInt(id)));
break;
case Constants.PUSH_ORDER:
intent = new Intent(context, OrderActivity.class);
intent.putExtra(Constants.SN, sn);
break;
case Constants.PUSH_SHOP:
intent = new Intent(context, IndexActivity.class);
intent.putExtra(Constants.FROM_PUSH,true);
break;
case Constants.PUSH_CATALOG:
intent = defaultIntent(context,new Catalog().setId(Integer.parseInt(id)));
break;
}
if (intent == null)
return;
Intent mainIntent = new Intent(context, IndexActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent[] intents = null;
if (SystemUtils.isAppAlive(context, Constants.PACKAGE_NAME)) {
LogUtils.logd("the app process is alive");
intents = new Intent[]{mainIntent,intent};
} else {
LogUtils.logd("the app process is dead");
Intent launchIntent = context.getPackageManager().
getLaunchIntentForPackage(Constants.PACKAGE_NAME);
launchIntent.setFlags(
Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
intents = new Intent[]{launchIntent, mainIntent, intent};
}
context.startActivities(intents);
}
}
注意:
Bundle bundle = PushManagerImpl.preHandlePush(context, intent);
這行代碼千萬(wàn)別少了,如果直接從 intent.getExtras(); 拿到的bundle對(duì)象是沒(méi)有數(shù)據(jù)的。
如果要從bundle中拿到j(luò)son格式數(shù)據(jù),控制臺(tái)在發(fā)送消息時(shí),在root params中要按照下面格式:
Android root params example : { "my_actions" : [ { "title" : "Pushwoosh", "url" : "https://www.pushwoosh.com" } ] }
然后通過(guò) String actions = bundle.getString("my_actions");拿到數(shù)據(jù)。
- 別忘了在xml中配置:
<receiver android:name="your.app.package.NotificationReceiver" />
<meta-data android:name="PW_NOTIFICATION_RECEIVER" android:value="your.app.package.NotificationReceiver"/>
ps: 如果你沒(méi)有定義PushReceiver的話,之前的自定義通知點(diǎn)擊后默認(rèn)跳入app主頁(yè),無(wú)論app進(jìn)程是否還在,都會(huì)走一遍Splash頁(yè)的。
推送服務(wù)注冊(cè)
在Application的onCreate()中:
final PushManager pushManager = PushManager.getInstance(context);
pushManager.setNotificationFactory(new CustomContentNotificationFactory());
try {
pushManager.onStartup(context);
} catch (Exception e) {
Log.e("Pushwoosh", e.getLocalizedMessage());
}
//Register for push!
pushManager.registerForPushNotifications();
ps:混淆規(guī)則
-keep class com.pushwoosh.** { *; }
-keep class com.arellomobile.** { *; }
-dontwarn com.pushwoosh.**
-dontwarn com.arellomobile.**
That's all ! Sharing creates value.