Creating a simple notification in android consists of few steps. NotificationCompat.Builder allow you set notification title, body and icon. then you can go forward to make it custom. See the below function. Call this where ever you want to use notification and set your custom values
private void createAndGenerateNotifcation() {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Notification TItile").setContentText("Body");
mBuilder.setAutoCancel(true);
try {
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"
+R.raw.yourfile
);
mBuilder.setSound(uri);
} catch (Exception e) {
e.printStackTrace();
}
// Intent resultIntent = new Intent(this, Notification.class);
Intent resultIntent = new Intent();
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(Notification.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify((int) System.currentTimeMillis(),
mBuilder.build());
}
Structure of Notification in Android
- Content title
- Large icon
- Content text
- Content info
- Small icon
- Time that the notification was issued. You can set an explicit value with setWhen(); if you don't it defaults to the time that the system received the notification.
Lets have a look on description
Setting Notification custom sound - Keep your file inside raw folder of res and give URI to notification like this try {
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"
+ R.raw.yourfile);
mBuilder.setSound(uri);
} catch (Exception e) {
e.printStackTrace();
}
You can set auto cancel to remove when user tap on this
mBuilder.setAutoCancel(true);
Starting your own activity while tap on notification - set your activity
name inside intent
Intent resultIntent = new Intent(this, Youractivty.class);
Reference URL
0 comments:
Post a Comment