use*_*741 14 android android-notifications parse-platform google-cloud-messaging
我知道Android中的推送通知声音可以自定义(在iOS上已经正常工作).
但是,我没有在文档中看到任何引用,只有iOS自定义声音.
我在Parse.com论坛上看到大约一年前要求提供这样的功能,并回答它是"在桌面上".
关于那个的任何更新?如果没有"正式"支持,任何已知的解决方法,以使其工作?
Chr*_*org 11
我想出了一个解决方案.这还不能通过Parse的API获得,但它们确实有解释如何扩展其ParsePushBroadcastReceiver的文档.
因此,创建一个扩展ParsePushBroadcastReceiver的类,onReceive调用方法generateNotification并编写自定义代码以创建自己的自定义通知.这样,您可以包含声音.首先,您需要将新的声音文件(ex mp3)添加到resources/res文件夹中的原始目录.
顺便说一句,不要忘记从清单中更改ParsePushBroadcastReceiver接收器以反映您的新文件.例:
<receiver android:name="com.parse.ParsePushBroadcastReceiver"
android:exported="false">
Run Code Online (Sandbox Code Playgroud)
至
<receiver android:name="com.*my_package_name*.MyBroadcastReceiver"
android:exported="false">
Run Code Online (Sandbox Code Playgroud)
这是我的代码.它有效并且可以重复使用.
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());
}
}
Run Code Online (Sandbox Code Playgroud)
本教程的最后解释了如何在推送通知上播放自定义声音。
这是使用这一行完成的:
notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");
Run Code Online (Sandbox Code Playgroud)