【问题标题】:Why opening activity by pushwoosh notification click doesn't work in android?为什么通过 pushwoosh 通知单击打开活动在 android 中不起作用?
【发布时间】:2018-06-28 20:16:50
【问题描述】:

我在我的项目中集成了PushWoosh。当用户点击通知时,我需要打开活动。收到推送后,我应该获取数据(比如说 id)并使用 Intent 发送此 id 并打开我的 Activity。 所以,我创建了工厂(用于自定义推送通知)并在GenerateNotification() 回调中创建了通知。但是,当我设置待定意图并单击通知后,它会打开我的主要活动。

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;

【问题讨论】:

    标签: android push-notification pushwoosh


    【解决方案1】:

    最后,我找到了解决方案。为了通过点击通知打开自定义活动,我们需要创建 BroadcastRecevier 并从此开始活动

    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" />
    

    【讨论】:

      【解决方案2】:

      删除PendingIntent pendingIntent = ...,然后添加:setNotifyIntent(pushIntent)

      final Intent pushIntent = new Intent(getContext().getApplicationContext(), PushActivity.class);
      ...
      setNotifyIntent(pushIntent);
      

      【讨论】:

        猜你喜欢
        • 2011-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多