2015-06-04 86 views
1

對不起,我的英語。我使用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:

我PU MP3到文件夾res/raw/beep1.mp3,並使用("android.resource://" + context.getPackageName() + R.raw.beep1);,但它不是幫助

回答

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

假設你有你自己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; 
} 

如果你只想改變聲音getNotification()方法是你需要overrid

只有一個