How do I send extra data through an urbanairship push notification

血红的双手。 提交于 2020-01-05 04:41:05

问题


So I need to send a push notification to a user's device. Then when the user clicks on the notification, I need my app to take a specific action. I want to include the parameter for the action in the notification. But I don't want the user to see the parameter; they should just see the message. Doing some research, I found the following on the urban airship website

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

So I am supposing that the alert portion is what a user sees. And that the portion under android could be the parameters. So my question is, in Java, how do I read those extra portions? such as story_id, or moar?


回答1:


Since there is no answer accepted here I thought I'll show how I am doing it. I have a class that extends BroadcastReceiver like you should if you are following UrbanAirship. In this class I recieve the notifications:

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

...

Might help someone:)




回答2:


You can extend BasicPushNotificationBuilder and override buildNotification. That method gets the extra parameters in 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);
    }
}

See docs here.




回答3:


Eran had the right idea, but you actually want to implement PushNotificationBuilder and then override buildNotification().

Something like this:

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


来源:https://stackoverflow.com/questions/21418936/how-do-i-send-extra-data-through-an-urbanairship-push-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!