android - Parse.com - Android 自定义推送通知声音

标签 android parse-platform google-cloud-messaging android-notifications

我知道推送通知声音在 Android 中可以自定义(在 iOS 上已经可以了)。

但是,我没有在文档中看到任何引用,仅针对 iOS 自定义声音。

我在 Parse.com 论坛上看到大约一年前有人要求这样的功能,并回答说它“在桌面上”。

关于这方面的任何更新?如果没有“官方”支持,是否有任何已知的解决方法可以使其正常工作?

最佳答案

我想出了一个解决办法。这还不能通过 Parse 的 API 获得,但他们确实有解释如何扩展他们的 ParsePushBroadcastReceiver 的文档。

因此创建一个扩展 ParsePushBroadcastReceiver 的类,并在 onReceive 调用方法 generateNotification 并编写自定义代码以在那里创建您自己的自定义通知。这样,您就可以包含声音。首先,您需要将新的声音文件(ex mp3)添加到 resources/res 文件夹中的原始目录。

顺便说一句,不要忘记更改 list 中的 ParsePushBroadcastReceiver 接收器以反射(reflect)您的新文件。示例:

    <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());
}
}

关于android - Parse.com - Android 自定义推送通知声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24526429/

相关文章:

android - 从 Windows Azure blob 下载图像?

ios - 从 UICollection 中删除项目但出现 "Array index out of range"错误

安卓 : not getting GCM Library in extra folder

amazon-web-services - 亚马逊 SNS : "Platform credentials are invalid" when re-entering a GCM API key that previously worked

android - 跟踪在 Android 中发送的短信

android - 如何从 Windows 修改 Genymotion VM 上的主机文件?

java - GSON : custom object deserialization

ios - 为什么我登录的 segue 不起作用?

node.js - 应该使用哪个 mailgun 库将 Parse 托管应用程序迁移到 Heroku?

android - 使用 php 获取 GCM 的 android 注册 ID