如何通过urbanairship推送通知发送额外数据

时间:2014-01-28 23:12:46

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

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

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

3 个答案:

答案 0 :(得分:3)

由于这里没有接受答案,我想我会说明我是怎么做的。 如果您关注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

...

可以帮助某人:)

答案 1 :(得分:0)

您可以展开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

答案 2 :(得分:0)

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;
    }
}