【问题标题】:parse custom push sound解析自定义推送声音
【发布时间】:2015-06-04 06:55:29
【问题描述】:

对不起我的英语。我使用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); 但它没有帮助

【问题讨论】:

    标签: java android parse-platform push


    【解决方案1】:

    假设您有自己的 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() 方法是你唯一需要覆盖的方法

    【讨论】:

      【解决方案2】:

      它对我有用! 导入:声音必须在这里: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());
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-13
        • 1970-01-01
        • 2020-07-23
        • 2016-09-16
        • 1970-01-01
        相关资源
        最近更新 更多