我想出了一个解决方案。这还不能通过 Parse 的 API 获得,但他们确实有说明如何扩展 ParsePushBroadcastReceiver 的文档。
因此,创建一个扩展 ParsePushBroadcastReceiver 的类,然后 onReceive 调用方法 generateNotification 并编写自定义代码以在那里创建您自己的自定义通知。这样,您可以包含声音。首先,您需要将新的声音文件(例如 mp3)添加到资源 / res 文件夹中的原始目录。
顺便说一句,不要忘记更改清单中的 ParsePushBroadcastReceiver 接收器以反映您的新文件。示例:
<receiver android:name="com.parse.ParsePushBroadcastReceiver"
android:exported="false">
到
<receiver android:name="com.*my_package_name*.MyBroadcastReceiver"
android:exported="false">
这是我的代码。它有效且可重复使用。
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());
}
}