【问题标题】:How do I send extra data through an urbanairship push notification如何通过 urbanairship 推送通知发送额外数据
【发布时间】:2014-01-28 23:12:45
【问题描述】:

所以我需要向用户的设备发送推送通知。然后,当用户单击通知时,我需要我的应用程序采取特定操作。我想在通知中包含操作的参数。但我不希望用户看到参数;他们应该只看到消息。做了一些研究,我在城市飞艇网站上发现了以下内容

{
   "audience": "all",
   "notification": {
      "alert": "Extras example",
      "android": {
        "extra": {
            "url": "http://example.com",
            "story_id": "1234",
            "moar": "{\"key\": \"value\"}"
         }
      }
   },
   "device_types": ["android"]
}

所以我假设alert 部分是用户看到的。而android 下的部分可能是参数。 所以我的问题是,在 Java 中,我如何阅读那些多余的部分? 例如story_idmoar

【问题讨论】:

  • 我已经编辑了问题的主要部分粗体。

标签: java android push-notification google-cloud-messaging urbanairship.com


【解决方案1】:

由于这里没有接受任何答案,我想我会展示我是如何做到的。 如果您正在关注 UrbanAirship,我有一个扩展 BroadcastReceiver 的课程。在这堂课中,我收到了通知:

public class IntentReceiver extends BroadcastReceiver {

    private static final String logTag = "PushUA";
    private String pushToken;

    public static String APID_UPDATED_ACTION_SUFFIX = ".apid.updated";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(logTag, "Received intent: " + intent.toString());
        String action = intent.getAction();

        if (action.equals(PushManager.ACTION_PUSH_RECEIVED)) {          

            int id = intent.getIntExtra(PushManager.EXTRA_NOTIFICATION_ID, 0);

            logPushExtras(intent);

        } else if (action.equals(PushManager.ACTION_NOTIFICATION_OPENED)) {

            Log.i(logTag, "User clicked notification. Message: " + intent.getStringExtra(PushManager.EXTRA_ALERT));

            logPushExtras(intent);

            String url= intent.getStringExtra("url"); //Here you get your extras

...

可能会帮助某人:)

【讨论】:

    【解决方案2】:

    您可以扩展BasicPushNotificationBuilder 并覆盖buildNotification。该方法获取extras 中的额外参数。

    @Override
    public Notification buildNotification(String alert, Map<String, String> extras) {
        // Only build inbox style notification for rich push messages
        if (extras != null && RichPushManager.isRichPushMessage(extras)) {
            return createRichNotification(alert);
        } else {
            return super.buildNotification(alert, extras);
        }
    }
    

    请参阅文档here

    【讨论】:

    • 感谢您的回复。 +1。但我实际上更关心如何阅读 android 端的附加内容,如 OP 中所述:So my question is, in Java, how do I read those extra portions?。很抱歉造成混乱。
    • Android 端的buildNotification 方法,它是Java 的。例如,extras.get("url") 将返回 "http://example.com"
    【解决方案3】:

    Eran 的想法是正确的,但您实际上想要实现 PushNotificationBuilder,然后覆盖 buildNotification()。

    类似这样的:

    /**
     * This class encapsulates notifications (those that appear in the notification shade).
     *
     * @author Karim Varela
     */
    public class ManagerNotifications implements PushNotificationBuilder
    {
        @Override
        public Notification buildNotification(String alert, Map<String, String> extras)
        {
            return null;
        }
    
        @Override
        public int getNextId(String alert, Map<String, String> extras)
        {
            return 0;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-09-18
      • 2011-01-24
      • 1970-01-01
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多