Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse.com - Android custom push notification sound

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?

like image 314
user1768741 Avatar asked Jul 02 '14 08:07

user1768741


3 Answers

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());
}
}
like image 65
ChrisBorg Avatar answered Nov 12 '22 05:11

ChrisBorg


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");
like image 39
Alan Avatar answered Nov 12 '22 04:11

Alan


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;
    }
}
like image 40
Max Worg Avatar answered Nov 12 '22 06:11

Max Worg