androidjsonnotificationspush-notificationurbanairship.com

Urbanairship customise push notification message content


I'm using urban airship to send push notification to my android application. I'm sending a json content from my backend server as push content

push is working correctly & I can get the push content. Push message is showing in the device notification bar as well.

But the problem is push notification shows the json content. I need to customise it & show a human readable message.

Here is the onPushReceived method in my IntentReceiver class ..

@Override
protected void onPushReceived(Context context, PushMessage message, int notificationId) {
    Log.i(TAG, "Received push notification. Alert: " + message.getAlert() + ". Notification ID: " + notificationId);

    String data = message.getAlert();
    try {
        JSONObject dataObj = new JSONObject( data );

        Logger.info("Data obj " + dataObj.toString());

        JsonDecoders.decodePushMessage(dataObj); // method to decode the json content

    } catch (JSONException e) {
        e.printStackTrace();
    }

    // try to build a new notification message.
    Intent intent = new Intent(context, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder b = new NotificationCompat.Builder(context);

    b.setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.mipmap.ic_launcher)
            .setTicker("Ticker")
            .setContentTitle("Title")
            .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
            .setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
            .setContentIntent(contentIntent)
            .setContentInfo("some info");


    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, b.build());

}

I can decode the push content.

I tried to add notification through NotificationManager , but then it shows two notifications in the notification bar. I need to override the content of the urban airship push. Is there any method or way to do it. I went through the documentation & couldn't find anything.


Solution

  • Urban Airship supports custom notifications but you need to let Urban Airship post the notificaiton. You will want to provide a custom notification factory instead of building a posting a notificaiton in the receiver. However you can probably avoid that by not sending JSON content as the messages alert, and instead send the extra data as an extra in the push payload.