Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Monday, August 1, 2016

How to set notification with custom sound in android

How to set notification with custom sound in android


I copied the mp3 (kalimba.mp3) file into raw folder in res folder.But when notification raised its getting default sound.Is there any error in the code.Could anyone please help me.

//on Button click i am calling this method///

protected void GenerateNotify() {        NotificationManager myNotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);        Notification notification=new Notification(android.R.drawable.ic_btn_speak_now,"hi",100);      Intent intent=new Intent(getApplicationContext(),as.class);      PendingIntent contentintent=PendingIntent.getBroadcast(getApplicationContext(),0, intent, 0);      notification.setLatestEventInfo(getApplicationContext(), "Hi","date", contentintent);      notification.flags |= Notification.FLAG_AUTO_CANCEL;      notification.sound = Uri.parse("android.resource://com.example.serviceproject/" + R.raw.kalimba);      myNotificationManager.notify(NOTIFICATION_ID,notification);  }  

Answer by QuokMoon for How to set notification with custom sound in android


notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd);  notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE;  

if defined DEFAULT_SOUND, then the default sound overrides any sound

Answer by dsandler for How to set notification with custom sound in android


R.raw.kalimba is an integer resource ID; you want the name of the sound resource in that Uri. So try:

notification.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE          + "://" + getPackageName() + "/raw/kalimba");  

Answer by bala for How to set notification with custom sound in android


notification.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE      + "://" + getPackageName() + "/raw/kalimba");  

Answer by Dipesh Adhikari for How to set notification with custom sound in android


try this:

Uri sound = Uri.parse("android.resource://" + context.getPackageName() + "/raw/notifysnd); notification.setSound(sound);

Answer by milan for How to set notification with custom sound in android


package com.example.android.alert;    import android.app.Notification; import  android.app.NotificationManager; import android.app.PendingIntent;  import android.content.Intent; import android.media.RingtoneManager;  import android.net.Uri; import  android.support.v4.app.NotificationCompat; import  android.support.v7.app.AppCompatActivity; import android.os.Bundle;  import android.support.v7.app.ActionBarActivity; import  android.view.View;    public class MainActivity extends AppCompatActivity {        NotificationCompat.Builder notification;      private static final int uniqueid = 123;        @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);            notification = new NotificationCompat.Builder(this);          notification.setAutoCancel(true);      }      public void clicked(View view){          //build the notification            //set notification sound          Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);          notification.setSound(uri);            notification.setSmallIcon(R.drawable.images);          notification.setTicker("yolo photo");          notification.setWhen(System.currentTimeMillis());          notification.setContentTitle("negative photo");          notification.setContentText("You are first to click a photo");            Intent intent = new Intent(this,MainActivity.class);          PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);          notification.setContentIntent(pendingIntent);            //build notification and send to device          NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);          nm.notify(uniqueid,notification.build());      } }  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.