解析自定义推送声音

时间:2015-06-04 06:55:30

标签: java android parse-platform push

抱歉我的英文。我使用parse.com我希望在推送时创建自定义声音。当我发送推送时,手机只会振动。没有声音,也没有显示消息。

这是我的声音:image(我没有15声望)

代码:

public class MyBroadcastReceiver extends ParsePushBroadcastReceiver {

    private int NOTIFICATION_ID = 1;

    @Override
    public void onPushOpen(Context context, Intent intent) {
        Intent i = new Intent(context, MainActivity.class);
        i.putExtras(intent.getExtras());
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }

    @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() + "/beep1.mp3"));


        mBuilder.setContentIntent(contentIntent);

        mNotifM.notify(NOTIFICATION_ID, mBuilder.build());
    }

}

UPD:

这也不行,我有标准的声音

@Override
    protected Notification getNotification(Context context, Intent intent) {
        Notification n = super.getNotification(context, intent);
        n.sound = Uri.parse("android.resource://" + context.getPackageName() + "beep1.mp3");
        return n;
    }

UPD:

我将mp3发送到文件夹res/raw/beep1.mp3并使用("android.resource://" + context.getPackageName() + R.raw.beep1);,但不提供帮助

2 个答案:

答案 0 :(得分:2)

假设您有自己的ParsePushBroadcastReceiver子类,并且您在Manifest中声明了它,我建议您将mp3文件放在/raw文件夹中并更改getNotification()方法,如下所示

@Override
protected Notification getNotification(Context context, Intent intent) {
    Notification n = super.getNotification(context, intent);
    n.defaults = 2;
    n.sound = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.beep1);
    return n;
}

如果您只想改变声音,getNotification()方法是唯一需要覆盖的方法

答案 1 :(得分:0)

它为我工作! 导入:声音如下:res/raw/beep1.mp3和此类路径"android.resource://" + context.getPackageName() +"/"+ "R.raw.beep1"

@Override
    public void onReceive(Context context, Intent intent) {
        try {

            JSONObject json = new JSONObject(intent.getExtras().getString(
            "com.parse.Data")); 

            alert = json.getString("alert").toString();

        } catch (JSONException e) {}

        notifySound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        mBuilder = new NotificationCompat.Builder(context);
        mBuilder.setSmallIcon(R.drawable.ic_launcher); //You can change your icon
        mBuilder.setContentText(alert);
        mBuilder.setContentTitle("Пользователь");
        mBuilder.setSound(Uri.parse("android.resource://" + context.getPackageName() +"/"+ R.raw.beep1));
        mBuilder.setAutoCancel(true);

        resultIntent = new Intent(context, MainActivity.class);

        PendingIntent resultPendingIntent = PendingIntent.getActivity(context,
                0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

                mBuilder.setContentIntent(resultPendingIntent);

                NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(context.NOTIFICATION_SERVICE);

                notificationManager.notify(mNotificationId, mBuilder.build());
        }
    }