我在我的项目中集成了PushWoosh
。我需要在用户点击通知时打开活动。当收到推送时,我应该获得数据(比如id)并使用Intent发送这个id并打开我的Activity。因此,我创建了工厂(用于自定义推送通知),并在GenerateNotification()
回调中创建了通知。但是当我设置Pending intent并点击通知后,它打开了我的主活动。
public class MyFactory extends AbsNotificationFactory {
private String id;
@Override
public Notification onGenerateNotification(PushData pushData) {
final String notificationTitle = "Title";
id = pushData.getExtras().getString("Id");
final Intent pushIntent = new Intent(getContext().getApplicationContext(), PushActivity.class);
pushIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
pushIntent.putExtra("Id", id);
final int uniqueId = Math.abs(UUID.randomUUID().hashCode());
PendingIntent pendingIntent = PendingIntent.getActivity
(getContext(), uniqueId, pushIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.setBigContentTitle(notificationTitle);
bigTextStyle.bigText(notificationAlert);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext())
.setSmallIcon(R.drawable.app_icon)
.setContentTitle(notificationTitle)
.setDefaults(Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent)
.setContentText(notificationAlert)
.setStyle(bigTextStyle);
final Notification notification = builder.build();
final NotificationManager notificationManager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
//notificationManager.notify(uniqueId, notification);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
return notification;
}
@Override
public void onPushReceived(PushData pushData) {
}
@Override
public void onPushHandle(Activity activity) {
}
}
但是重定向工作如果我把
notificationManager.notify(uniqueId, notification);
return null;
我终于找到了解决办法。为了通过点击通知打开自定义活动,我们需要创建broadcastreceiver并从这个
开始活动public class PushNotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent incomingIntent) {
if (incomingIntent == null)
return;
// Get data here if need
// From incomingIntent and PushBundle
Intent intent = new Intent(context,PushReceiverActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
//Let Pushwoosh SDK post-handle push (track stats, etc.)
PushManagerImpl.postHandlePush(context, incomingIntent);
}
}
别忘了把它添加到清单
<receiver android:name=".PushNotificationReceiver" />
<meta-data
android:name="PW_NOTIFICATION_RECEIVER"
android:value="com.example.test.pushwooshtest.PushNotificationReceiver" />
移除PendingIntent pendingIntent = ...
,再添加:setNotifyIntent(pushIntent)
final Intent pushIntent = new Intent(getContext().getApplicationContext(), PushActivity.class);
...
setNotifyIntent(pushIntent);