2016-07-22 107 views
0

我在PostFragment.java在Github上链接的代码如下如何创建自定义声音通知在Android的

https://gist.github.com/sayseee/0801ff9d955e333365bbd6a73084db71

在该代码中我已经有一个代码sendToWear()其上单击Works

//Send a BigTextStyle notification with text contents of the post to Android Wear devices 
private void sendToWear() { 
    // Intent used to run app on the phone from watch 
    Intent viewIntent = new Intent(getActivity(), MainActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), 0, viewIntent, 0); 

    // Use BigTextStyle to read long notification 
    //NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle(); 
    //bigTextStyle.setBigContentTitle(title); 
    // Use Html.fromHtml() to remove HTML tags 
    //bigTextStyle.bigText(Html.fromHtml(content)); 

    final NotificationCompat.Builder builder = new NotificationCompat.Builder(getActivity()); 
    builder.setSmallIcon(R.mipmap.ic_launcher) 
      .setContentTitle(title) 
      .setContentIntent(pendingIntent); 
      //.setStyle(bigTextStyle); 

    // Load featured image as background image 
    Glide.with(this) 
      .load(featuredImageUrl) 
      .asBitmap() 
      .centerCrop() 
      .into(new SimpleTarget<Bitmap>(160, 160) { 
       @Override 
       public void onResourceReady(Bitmap resource, 
              GlideAnimation<? super Bitmap> glideAnimation) { 
        builder.setLargeIcon(resource); 

        NotificationManagerCompat notificationManagerCompat = 
          NotificationManagerCompat.from(getActivity()); 
        notificationManagerCompat.cancel(id); 
        notificationManagerCompat.notify(id, builder.build()); 
       } 
      }); 
} 

当我发布新内容时,我在哪里插入通知代码并为新提要添加声音?像

enter image description here

回答

0

试试这个,

替换为builder对象,

final NotificationCompat.Builder builder = new NotificationCompat.Builder(getActivity()); 
builder.setSmallIcon(R.mipmap.ic_launcher) 
     .setContentTitle(title) 
     .setContentIntent(pendingIntent) 
     .setSound(Uri.parse("android.resource://" 
       + getActivity().getPackageName() + "/" + R.raw.your_custom_sound_file)) ; 
+0

THEX @Pramod,我得到的代码,我在网上上方,似乎将数据发送到android可穿戴设备。我真的是新的android我想上面的代码和我可以插入它的位置..你可以检查上面的github链接 –

+0

尝试更新的回答 –

+0

我是否将该代码添加到'private void sendToWear()'或者我创建一个新的一个? –