14

I know that push notification sound, in Android, can be customised (on iOS already working).

However, I don't see any reference in the docs, only per iOS custom sound.

I saw in Parse.com forum that such a feature was requested about a year ago and answered that it was "on the table".

Any updates regarding that? If not "officially" supported, any known workaround to get it working?

rici
  • 234,347
  • 28
  • 237
  • 341
user1768741
  • 155
  • 1
  • 7

3 Answers3

11

I figured out a solution. This is not available through Parse's API yet but they do have documentation which explains how to extend their ParsePushBroadcastReceiver.

So create a class which extends the ParsePushBroadcastReceiver, and onReceive call a method generateNotification and write the custom code to create a custom notification of your own there. This way, you can include a sound. First of all, you would need to add the new sound file (ex mp3) to a raw directory in the resources / res folder.

By the way, don't forget to change the ParsePushBroadcastReceiver receiver from the manifest to reflect your new file. Example:

    <receiver android:name="com.parse.ParsePushBroadcastReceiver"
        android:exported="false">

to

    <receiver android:name="com.*my_package_name*.MyBroadcastReceiver"
        android:exported="false">

Here's my code. It works and it's reusable.

public class MyBroadcastReceiver extends ParsePushBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    try {
        String jsonData = intent.getExtras().getString("com.parse.Data");
        JSONObject json = new JSONObject(jsonData);

        String title = null;
        if(json.has("title")) {
            title = json.getString("title");
        }

        String message = null;
        if(json.has("alert")) {
            message = json.getString("alert");
        }

        if(message != null) {
            generateNotification(context, title, message);
        }
    } catch(Exception e) {
        Log.e("NOTIF ERROR", e.toString());
    }
}


private void generateNotification(Context context, String title, String message) {
    Intent intent = new Intent(context, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);

    NotificationManager mNotifM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    if(title == null) {
        title = context.getResources().getString(R.string.app_name);
    }

    final NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.icon)
                    .setContentTitle(title)
                    .setContentText(message)
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(message))
                    .addAction(0, "View", contentIntent)
                    .setAutoCancel(true)
                    .setDefaults(new NotificationCompat().DEFAULT_VIBRATE)
                    .setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.whistle));

    mBuilder.setContentIntent(contentIntent);

    mNotifM.notify(NOTIFICATION_ID, mBuilder.build());
}
}
ChrisBorg
  • 1,418
  • 17
  • 27
  • Two good things about this solution is that 1) it gives the user and application the ability to cancel the notification since the ID is known and 2) it doesn't call `super.onRecieve` so that there won't be a duplicate, parse-SDK-generated notification. – Max Worg Feb 26 '15 at 11:35
  • @ChrisBorg I'm getting an error on the line with `mNotifM.notify(NOTIFICATION_ID, mBuilder.build));` saying that NOTIFICATION_ID can not be resolved to a variable. What am I missing? – Phil Mar 17 '15 at 13:14
  • 1
    That NOTIFICATION_ID can be any integer. It is normally used so you can manage that notification later after its presented. In this case set any integer or a generated random number – ChrisBorg Mar 17 '15 at 13:36
  • This should be the accepted answer, its precise and reusable. – Joel Sep 01 '15 at 15:45
  • I am using PHP API to send android parse push notification, in that case how to do ? – Mohini Oct 21 '15 at 13:09
  • @Mohini I believe that custom sounds still has to be achieved from the app itself and cannot be done from an API – ChrisBorg Oct 22 '15 at 08:42
  • @ChrisBorg, thanks for this code , my time saved, its working – Mohini Oct 23 '15 at 09:53
1

At the end of this tutorial is explained how to play custom sounds on the push notifications.

It is done using this line:

 notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");
Alan
  • 1,509
  • 1
  • 16
  • 21
1

Another option to provide sound without having to generate your own notification is to just add a sound to the notification that Parse already creates for you like this:

public class MyParsePushBroadcastReceiver extends ParsePushBroadcastReceiver {

   @Override
    protected Notification getNotification(Context context, Intent intent) {
        Notification n = super.getNotification(context, intent);
        n.sound = Uri.parse("android.resource://" + context.getPackageName() + "/some_sound.mp3");
        return n;
    }
}
Max Worg
  • 2,932
  • 2
  • 20
  • 35
  • This was the answer I had come up with on my own, and I was excited to see someone else had posted it as a working solution. Unfortunately, on my LG G3 running Lollipop (5.0.1), I still get the default sound. Any ideas? – mbm29414 Mar 25 '15 at 22:19