Preview Notification on android - android

I'm using this code to show a notification to user from code :
Intent intent = new Intent(this, this.getClass());
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.icon)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.icon))
.setContentTitle("Notification!")
.setContentText("Hello word")
.setContentIntent(pi)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_MAX);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
This is juste adding the notification in the notification screen (and also the icon of my appliaction in the status bar)
But how can I have a preview of this notification on top of screen ?

Related

Push Notification with action button is not working

i have used the fcm for notification and its coming correctly, but when i try to add add buttons in notification its not showing.
my code
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("New Message From Film")
.setContentText(messageBody)
.setAutoCancel(true)
.addAction(R.drawable.pp, "Accept",pendingIntent)
.addAction(R.drawable.pp, "Reject",pendingIntent)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
You forgot to build use method build() in last
edited code
Builder
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("New Message From Film")
.setContentText(messageBody)
.setAutoCancel(true)
.addAction(R.drawable.pp, "Accept",pendingIntent)
.addAction(R.drawable.pp, "Reject",pendingIntent)
.setContentIntent(pendingIntent);
notification
final Notification notification = notificationBuilder.build();
mNotificationManager.notify(notificationId, notification);

How create Android different notification every time?

Notification work normally one problem is All notification go to last notification link. How can I fix it? Every notification go to it's own link. Every message notification go to he's/she's inbox. How can I do that?
Intent intent_activity_start = new Intent(this,MainActivity.class);
intent_activity_start.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent_activity_start.putExtra("links", c.getString("link"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent_activity_start,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.logo);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
builder.setLargeIcon(getCircleBitmap(photoStream(c.getString("profile_photo"))));
builder.setContentTitle("Title");
builder.setContentText(c.getString("notify"));
builder.setSound(notificationSound);
builder.setStyle(new NotificationCompat.BigTextStyle()
.bigText(c.getString("notify")));
if(!c.getString("photo").equals("null")){
builder.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(photoStream(c.getString("photo"))));
}
NotificationManager notificationManager = (NotificationManager) getSystemService(
NOTIFICATION_SERVICE);
notificationManager.notify(counter.incrementAndGet(), builder.build());

Android notification Big View - Intent on button not fired

I'm following the Android Dev tutorial to use Big View for notifications. I want an on going notification with some functionality when clicking on the body of the notification and other functionality when clicking the Button.
This is my code:
Intent bodyIntent = new Intent(context, MainActivity.class);
innerIntent.putExtra(NOTIFICATION_BODY, "Click on notification body");
PendingIntent bodyPendingIntent = PendingIntent.getActivity(context, 0, bodyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent buttonIntent = new Intent(context, MainActivity.class);
buttonIntent.setAction(NOTIFICATION_BUTTON);
PendingIntent buttonPendingIntent = PendingIntent.getService(context, 0, buttonIntent, 0);
android.support.v4.app.NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setCategory(Notification.CATEGORY_SERVICE)
.setPriority(Notification.PRIORITY_MAX)
.setVisibility(Notification.VISIBILITY_PRIVATE)
.setContentIntent(notificationPendingIntent)
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.setLights(getResources().getColor(R.color.primary), 50, 10000)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setVibrate(new long[] {0, 50})
.setOngoing(true)
.setStyle(new NotificationCompat.BigTextStyle().bigText("Hello World BIG!"))
.addAction(R.mipmap.ic_image, "Button", buttonPendingIntent);
int mNotificationId = 001;
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
Then in onResume:
Intent intent = getIntent();
Log.e("m", String.valueOf(intent.getIntExtra(NOTIFICATION_RESULT, 0)));
Log.e("m", String.valueOf(intent.getAction()));
Result:
When clicking on the body of the notification the bodyIntent fires and I get the correct log printing.
When clicking on the button: Nothing happens and MainActivity not even starting.
Thanks,
You should call PendingIntent.getActivity() method if you want to start an activity, but you are creating buttonPendingIntent by calling PendingIntent.getService().

Notification doesn't autoCancel in 5.0

I'm trying to Cancel Notification on Tap of Notification which is working perfectly fine below 5.0. But somehow that same notification is not autoCancel in Android 5.0
Here's what i used to cancelNotification (Works smoothly < 5.0)
PendingIntent resultPendingIntent = PendingIntent.getActivity(ctx,
uniqueID, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_mobcast_notification)
.setContentTitle(message)
.setContentText(title)
.addAction(android.R.drawable.ic_menu_gallery, title,
resultPendingIntent);
mBuilder.setDefaults(-1);
mBuilder.setOnlyAlertOnce(true);
mBuilder.setAutoCancel(true);
mBuilder.setContentIntent(resultPendingIntent);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Notification notification = new NotificationCompat.BigPictureStyle(
mBuilder).bigPicture(
BitmapFactory.decodeFile(mImagePath, options)).build();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(434, notification);
Tried :-
Try to cancel the notification where the pendingIntent leads to. But no still notification is there on Notification Bar(5.0)
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancelAll();
What could be the Extra Flag which i missed out to set for 5.0 will autoCancel notification on Tap of it?
I used following code, it works perfect, you can try:
public void showNotification(View view) {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle("Message")
.setContentText("New Message")
.setTicker("Alert New Message")
.setSmallIcon(R.drawable.ic_launcher);
Intent moreInfoIntent = new Intent(this, MoreInfoNotification.class);
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(this);
taskStackBuilder.addParentStack(MoreInfoNotification.class);
taskStackBuilder.addNextIntent(moreInfoIntent);
PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);
notificationBuilder.setAutoCancel(true);
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notifId, notificationBuilder.build());
isNotificActive = true;
}

Notification led not working when using NotificationCompat?

In my notification, i want the led to blink in white color.
I am using NotificationCompat class from the support library to build the notification.
Notification fires up nicely, but led doesn't blink. (tested on Nexus 4)
Here's the code:
// Build notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.ic_stat_notify);
mBuilder.setTicker(getResources().getString(R.string.task_due));
mBuilder.setContentTitle(notify_title).setContentText(notify_descrip);
mBuilder.setDeleteIntent(deleteIntent());
mBuilder.setPriority(Notification.PRIORITY_DEFAULT);
mBuilder.setLights(0xFFFF0000, 500, 500);
mBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
// On notification tap
Intent resultIntent = new Intent(this, SpecialActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
// Notify the user
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());

Categories

Resources