1

Well, I'm trying to use notifications, but this code doesn't work. I've tested it on 4.4 and 5.0. I can't understand, what is wrong.

public void onClick(View view) { Context context = getApplicationContext(); Intent notificationIntent = new Intent(context, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(context, 0,notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); Resources res = context.getResources(); Notification.Builder builder = new Notification.Builder(context); builder.setContentIntent(contentIntent) .setWhen(System.currentTimeMillis()) .setAutoCancel(true) .setContentTitle("Title") .setContentText("Text"); Notification notification = builder.build(); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(NOTIFY_ID, notification); } 

I'll be grateful for answer.

1 Answer 1

1

It is probably because Google changes its notification API quite a lot in each Android Edition. So the API you used not compatible for multi Android version. But Google release appcompat-v7 \appcompat-v4 to solve this stuff.

Try the following code:

public void send(View v) { NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Context context = getApplicationContext(); Intent notificationIntent = new Intent(context, TestNotificationActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(this); Notification notification = builder .setContentIntent(contentIntent) .setContentTitle("this is title") .setContentText("this is content") .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .build(); manager.notify(NOTIFY_ID, notification); } 

Remember to import android.support.v7.app.NotificationCompat.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.