【问题标题】:Parse.com - Android custom push notification soundParse.com - Android 自定义推送通知声音
【发布时间】:2014-07-02 08:32:44
【问题描述】:

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

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

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

对此有任何更新吗?如果不是“官方”支持,是否有任何已知的解决方法可以让它工作?

【问题讨论】:

  • 你得到这个问题的答案了吗?我也面临同样的问题。

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


【解决方案1】:

我想出了一个解决方案。这还不能通过 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());
}
}

【讨论】:

  • 这个解决方案的两个好处是 1) 它使用户和应用程序能够取消通知,因为 ID 是已知的;2) 它不调用 super.onRecieve,因此赢得了'不是重复的、解析 SDK 生成的通知。
  • @ChrisBorg 我收到一条错误消息,mNotifM.notify(NOTIFICATION_ID, mBuilder.build)); 表示 NOTIFICATION_ID 无法解析为变量。我错过了什么?
  • NOTIFICATION_ID 可以是任何整数。它通常被使用,因此您可以在其呈现后稍后管理该通知。在这种情况下,设置任何整数或生成的随机数
  • 这应该是公认的答案,其精确且可重复使用。
  • 我正在使用PHP API发送android解析推送通知,这种情况下怎么办?
【解决方案2】:

tutorial 的末尾解释了如何在推送通知上播放自定义声音。

使用这条线完成:

 notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");

【讨论】:

    【解决方案3】:

    无需生成您自己的通知即可提供声音的另一种选择是向 Parse 已经为您创建的通知添加声音,如下所示:

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

    【讨论】:

    • 这是我自己想出的答案,我很高兴看到其他人将其发布为可行的解决方案。不幸的是,在我运行 Lollipop (5.0.1) 的 LG G3 上,我仍然听到默认声音。有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    • 2020-07-23
    • 2016-09-16
    相关资源
    最近更新 更多