Saturday, 8 June 2013

Android Custom Notification Sound with Important Message


For important messages to be given to the user, it is required to have more persistent method. A notification is a message you can display as an icon at the top of the device which we call notification bar or status bar.

 MainActivity


import org.apache.cordova.DroidGap;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends DroidGap {

   
    Button btn_beep;
    MediaPlayer mMediaPlayer = new MediaPlayer();

    NotificationManager mNotificationManager;
    int notificationID = 1;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
   

       
        btn_beep = (Button)findViewById(R.id.btn_beep);
        btn_beep.setOnClickListener(new View.OnClickListener() {
           
            @Override
            public void onClick(View v) {
               

                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                     public void run() {
                       
                 
                         Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                            Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
                            r.play();   
                     }
                }, 100);

 //===============or============




        //        displayNotification("Welcome chandru");   //another one
               
            }
        });
    }
   
    protected void displayNotification(String Message)
    {
         Log.i("Start", "notification");

       
            Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
       
          // Invoking the default notification service //
            NotificationCompat.Builder  mBuilder =
                    new NotificationCompat.Builder(this);
            mBuilder.setAutoCancel(true);

            mBuilder.setContentTitle("New Message");
            mBuilder.setContentText("You have "+Message+"new message.");
            mBuilder.setTicker("New message from PayMe..");
            mBuilder.setSound(notification);
            mBuilder.setSmallIcon(R.drawable.ic_launcher);

          // Increase notification number every time a new notification arrives //

          // Creates an explicit intent for an Activity in your app //

            Intent resultIntent = new Intent(this, MainActivity.class);

            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            stackBuilder.addParentStack(MainActivity.class);

          // Adds the Intent that starts the Activity to the top of the stack //
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent =
                    stackBuilder.getPendingIntent(
                            0,
                            PendingIntent.FLAG_UPDATE_CURRENT
                    );
            mBuilder.setContentIntent(null);

          //  mBuilder.setOngoing(true);
            Notification note = mBuilder.build();
            note.defaults |= Notification.DEFAULT_VIBRATE;
            note.defaults |= Notification.DEFAULT_SOUND;
           
            mNotificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
          // notificationID allows you to update the notification later on. //
            mNotificationManager.notify(notificationID, mBuilder.build());

        }
   
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

No comments:

Post a Comment