简体   繁体   English

解析自定义推送声音

[英]parse custom push sound

Sorry for my english. 对不起我的英语不好。 I use parse.com i want create custom sound when i get push. 我使用parse.com我想在推送时创建自定义声音。 When I send a push, the phone just vibrates. 当我发送推送消息时,电话只会振动。 There is no sound and the message does not show too. 没有声音,消息也没有显示。

This is my sound: image (i have not 15 reputation) 这是我的声音: 图片 (我没有15个声誉)

code: 码:

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: UPD:

This is not work too, i have standart sound 这也行不通,我有标准声音

@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: UPD:

I pu mp3 to folder res/raw/beep1.mp3 and use ("android.resource://" + context.getPackageName() + R.raw.beep1); 我将mp3放到res/raw/beep1.mp3文件夹并使用("android.resource://" + context.getPackageName() + R.raw.beep1); but its not help 但它没有帮助

Assuming that you have your own subclass of ParsePushBroadcastReceiver and you declared it in Manifest, I recommend you to put your mp3 file in the /raw folder and to change your getNotification() method as follows 假设您有自己的ParsePushBroadcastReceiver子类,并且已在清单中声明了它,建议您将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;
}

If you want just to change sound the getNotification() method is the only one you need to overrid 如果只想更改声音,则只需要重写getNotification()方法即可。

Its work for me! 它为我工作! Import : sound mus be here: res/raw/beep1.mp3 and path like this "android.resource://" + context.getPackageName() +"/"+ "R.raw.beep1" 导入 :声音在此处: 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());
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM