【问题标题】:Parse.com - custom notifications vs setDefaultPushCallback - starting wrong activityParse.com - 自定义通知与 setDefaultPushCallback - 开始错误的活动
【发布时间】:2014-08-20 01:30:43
【问题描述】:

目标

  • 带有 Parse 后端的移动应用程序
  • 用户向所有/管理员/特定用户创建消息
  • “收件人”收到收到新消息的通知
  • 点击通知时,会显示有关消息的详细信息

目前的方法

我在 afterSave 云代码方法中创建了一个自定义推送。调用如下所示:

Parse.Push.send({
    where : query,
    data : {
        action : "com.acme.CUSTOM_ACTION_GOES_HERE",
        content : messageContent
    }
}).then(...

(查询被定义为获取特定的Parse.Installation对象。)

在 Android 应用中,我有一个像这样注册的 BroadcastReceiver

<receiver
    android:name=".MyBroadcastReceiver"
    android:exported="false" >
    <intent-filter>
        <action android:name="com.acme.CUSTOM_ACTION_GOES_HERE" >
        </action>
    </intent-filter>
</receiver>

在广播接收器的 onReceive 方法中,我创建了自定义通知,我还定义了在单击通知时启动活动的待定意图:

Intent contentIntent = new Intent(context, DisplayDetailsActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(DisplayDetailsActivity.class);
stackBuilder.addNextIntent(contentIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Notification noti = new Notification.Builder(context)
.setContentTitle("New message")
.setContentText(content)
.setSmallIcon(R.drawable.ic_stat_notify_message)
.setNumber(notificationsCount)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build();
noti.defaults |= Notification.DEFAULT_SOUND;
NotificationManager mgr = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
mgr.notify(555, noti);

问题

不调用(例如在应用程序的onCreate 方法中)

PushService.setDefaultPushCallback(getApplicationContext(), SomeActivity.class);

...推送未发送到设备

使用该调用时,当点击通知时会启动活动SomeActivity,而不是在广播接收器的意图中设置的活动DisplayDetailsActivity

当我只有一种类型的自定义通知时,可以使用DisplayDetailsActivity.class 作为第二个参数来调用setDefaultPushCallback

但是,我计划有多个不同的自定义通知,单击每个通知应该会启动不同的活动。在这种情况下,上述解决方法不再可行。

任何想法如何解决这个问题?

【问题讨论】:

    标签: android notifications parse-platform


    【解决方案1】:

    在为此工作了几个小时后,我偶然发现了这篇文章,我从@pareshgoel 解决方法开始,基本思想是消除注册活动或订阅频道(如果不需要)的需要,并降低Installation 类的服务器请求。

    我从 Parse.com 获取 Parse-1.5.1.jar,并使用 JD-GUI 我查看了PushService.setDefaultPushCallback(Context context, Class&lt;? extends Activity&gt; cls)

    它的作用是

    if cls is null remove subscription; create subscription for new cls; call PushService.startServiceIfRequired

    函数PushService.startServiceIfRequired在设备不支持GCM或向GCM发送注册请求时启动解析推送服务,等待registration_id,到达后更新安装设备token。

    我的解决方法是使用:PushService.startServiceIfRequired - 似乎没有记录,您可以在初始化之后使用,或者如果您在登录/注册后有用户,因为它使用后台任务,它可以被调用在主线程上。

    另一个优点是,使用PushService.startServiceIfRequired,您有 2 个服务器请求来保存/更新Installation 类,第一个将推送类型设置为 GCM,第二个将推送类型设置为 GCM,将设备令牌设置为 registration_id。 PushService.setDefaultPushCallback 引入了初始服务器请求。

    【讨论】:

    • 我的一个设备就是收不到推送通知。这使它再次焕发生机,谢谢! :)
    【解决方案2】:

    我遇到了同样的问题,并且有一个解决方法。

    首先需要调用 setDefaultPushCallback 才能使 Parse 推送通知正常工作。这很可能是 Parse 的错误。我在 Parse Android 1.5.1 上。

    解决方法是不在解析推送通知发送代码中设置“alert”参数。 如果未设置,则 Parse 不会自行显示警报,您的自定义广播接收器可以处理推送。

    Parse 中的推送通知需要更多的 bug 修复才能可靠使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-04
      相关资源
      最近更新 更多