轉(zhuǎn)載請注明原作者,如果你覺得這篇文章對你有幫助或啟發(fā),可以關(guān)注打賞。
Pushwoosh是境外的一個提供免費推送服務(wù)的公司,Android app當然也是基于Google Cloud Messaging 封裝的。
因為官方文檔不太直觀,也可能是之前不太了解過國外此類第三方服務(wù)的套路,使用過程也遇到了一些小問題,算是采坑了吧!測試也請使用包含完整Google服務(wù)框架的真機。
添加依賴
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é)點下添加:
<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)建應用的ID
PW_PROJECT_ID是從Google開發(fā)者控制臺設(shè)置GCM拿來的工程號,
notice: 你需要給這個工程號手動添加前綴“A”。
NotificationFactory
自定義NotificationFactory繼承DefaultNotificationFactory,實現(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;
}
}
這一步定義了通知的樣式,這樣我們收到推送后就會顯示自定義的通知樣式及數(shù)據(jù),但一般我們需要在點擊通知后跳轉(zhuǎn)到app指定的頁面,這里大家就不要想著給通知添加PendingIntent了,當然添加7.0的Action是可以的。
PushReceiver
- 自定義PushReceiver繼承BroadcastReceiver,這樣收到推送彈出通知,點擊通知后就會進入PushReceiver的onReceive(),在這里實現(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);
這行代碼千萬別少了,如果直接從 intent.getExtras(); 拿到的bundle對象是沒有數(shù)據(jù)的。
如果要從bundle中拿到j(luò)son格式數(shù)據(jù),控制臺在發(fā)送消息時,在root params中要按照下面格式:
Android root params example : { "my_actions" : [ { "title" : "Pushwoosh", "url" : "https://www.pushwoosh.com" } ] }
然后通過 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: 如果你沒有定義PushReceiver的話,之前的自定義通知點擊后默認跳入app主頁,無論app進程是否還在,都會走一遍Splash頁的。
推送服務(wù)注冊
在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.