user2568702
user2568702

Reputation:

Create Notification App in Android?

I want to create a sample project to show notification on click of button then when the user selects it open that activity in my app or open a url on selection of that. I have done something but I'm unable to complete the functionality.

First I am getting error to use this: @SuppressLint("NewApi")

If I am not using this I am getting the error on here

Notification noti = new Notification.Builder(this)

Activity Code

public class NotificationExample extends Activity implements OnClickListener{

    private Button b;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification_example);
        b=(Button)findViewById(R.id.button1);
        b.setOnClickListener(this);

    }


    @SuppressLint("NewApi")
    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
        Notification noti = new Notification.Builder(this)
        .setTicker("Ticker Title")
        .setContentTitle("Content Title")
        .setContentText("Notification content.")
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentIntent(pIntent).getNotification();
        noti.flags=Notification.FLAG_AUTO_CANCEL;
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0, noti); 
    }
}

Why I have to use this:

`@SuppressLint("NewApi")` 

in my code? I am not getting the notification sound. Please suggest me what changes I have to make in my code.

Upvotes: 1

Views: 2481

Answers (3)

Chintan Rathod
Chintan Rathod

Reputation: 26034

To add sound to your notification, use following short of code.

Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(notificationSound);

Full Code

PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
                0);
Builder builder = new NotificationCompat.Builder(context)
                .setTicker("Ticker Title").setContentTitle("Content Title")
                .setContentText("Notification content.")
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(pIntent);
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(notificationSound);
Notification noti = builder.build();
noti.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);

context is Application Context get using getApplicationContext().

Edit

To open any link to Browser using Notification, use following code.

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.google.com"));

Pass this intent to your PendingIntent

Upvotes: 2

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

Try this Function

public static void addToNotificationBar(Context mContext,String ticker, String title, String message) {
        long when = System.currentTimeMillis();
        int icon = R.drawable.ic_launcher;

        NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, new Intent(), 0);
        Notification notification = new Notification(icon, ticker, when);
        notification.setLatestEventInfo(mContext, title, message, contentIntent);
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, notification);
    }

Upvotes: 0

Andro Selva
Andro Selva

Reputation: 54332

Try this,

replace

Notification noti = new Notification.Builder(this);

with

Notification noti = new Notification.Builder(NotificationExample .this);

You can't use this reference for context object from a click listener.

Upvotes: 1

Related Questions