I need use notification with click event, i have notification method but this method don't open my activity.
My code:
private void sendNotification(String msg) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setContentTitle("EXX")
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg)
.setOngoing(true);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
is this possible,
thanks.
Yes, it is possible.
Change Your method, like that;
private void sendNotification(String msg) {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("yourpackage.notifyId", NOTIFICATION_ID);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setContentTitle("EXX")
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.addAction(getNotificationIcon(), "Action Button", pIntent)
.setContentIntent(pIntent)
.setContentText(msg)
.setOngoing(true);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
and add your mainactivity
NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
this code working.
Hey #John it's very simple
you just have to do
Intent intent = new Intent(this, ResultActivity.class);
...
// Because clicking the notification opens a new ("special") activity, there's
// no need to create an artificial back stack.
PendingIntent pendingIntent = TaskStackBuilder.create(getApp())
.addNextIntent(intent)
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
And set pending Intent in mBuilder
private void sendNotification(String msg) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setContentTitle("EXX")
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg)
.setOngoing(true);
.setContentIntent(pendingIntent)
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
and you done :)
Related
when I receive the notifications, it comes without time, never showing me hour, find the below attached code.
This is my notification code
public void showBigNotification(String title, String message, String url, Intent intent) {
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mCtx,
ID_BIG_NOTIFICATION,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
bigPictureStyle.setBigContentTitle(title);
bigPictureStyle.setSummaryText(Html.fromHtml(message).toString());
bigPictureStyle.bigPicture(getBitmapFromURL(url));
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx);
Notification notification;
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notification = mBuilder.setSmallIcon(R.mipmap.ic_launcher).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentIntent(resultPendingIntent)
.setContentTitle(title)
.setStyle(bigPictureStyle)
.setSound(defaultSoundUri)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.mipmap.ic_launcher))
.setContentText(message)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(ID_BIG_NOTIFICATION, notification);
}
Replace 0 with System.currentTimeMillis() in .setWhen() function.
.setWhen(System.currentTimeMillis())
I'm trying to start an activity by clicking on a notification but that only works when I'm already in the app.
Here's the code I think has to do with it:
private void sendNotification(String body) {
Intent intent = new Intent(ImpFirebaseMessagingService.this, codeActivity.class);
intent.putExtra("body", body);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Implementation TFA")
.setContentText(body)
.setAutoCancel(true)
.setSound(notificationSound)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
The following works in my app:
private void sendReminderNotification(Context context) {
Resources resources = context.getResources();
Intent notificationIntent = new Intent(context, TitleActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setSmallIcon(android.R.drawable.ic_lock_idle_alarm)
.setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.my_diary_launcher_icon))
.setContentTitle("MyDiary")
.setContentText("Reminder to input entry.")
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, builder.build());
}
Use pendingIntent to pass data of intent:
private void sendReminderNotification(Context context) {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Implementation TFA")
.setContentText(body)
.setAutoCancel(true)
.setSound(notificationSound);
Intent intent = new Intent(ImpFirebaseMessagingService.this, codeActivity.class);
intent.putExtra("body", body);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
public class MyBroadcaseciver extends BroadcastReceiver {
MediaPlayer mymedia;
#Override
public void onReceive(Context context, Intent intent) {
mymedia=MediaPlayer.create(context,R.raw.alarm);
mymedia.start();
Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();
}
}
The code above is my broadcast receiver, which plays a song when it fires. This is working as expected, however I wish to call a push notification to pop here and also my notification isn't working here.
Notification.Builder bulider = new Notification.Builder(this)
.setContentTitle("Rainfall Alert")
.setContentText("Todays Rain");
Notification notification = bulider.build();
NotificationManager manager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0,notification);
Help is appreciated.
public void sendNotification(String title, String body, Intent intent,int pushid) {
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(body))
.setContentText(body)
.setAutoCancel(true)
.setSound(defaultSoundUri);
if(intent!=null) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
notificationBuilder.setContentIntent(pendingIntent);
}
NotificationManager notificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
call this funcation in onrecive method
Intent intent = new Intent(context, Mainscreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
sendNotification("title","message", intent, 0);
public class MyBroadcaseciver extends BroadcastReceiver {
MediaPlayer mymedia;
#Override
public void onReceive(Context context, Intent intent) {
mymedia=MediaPlayer.create(context,R.raw.alarm);
mymedia.start();
Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();
Intent intent = new Intent(context, Mainscreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
sendNotification("title","message", intent, 0);
}
public void sendNotification(String title, String body, Intent intent,int pushid) {
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(body))
.setContentText(body)
.setAutoCancel(true)
.setSound(defaultSoundUri);
if(intent!=null) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
notificationBuilder.setContentIntent(pendingIntent);
}
NotificationManager notificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
i am using this code for sending local notifications
mNotificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
i, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setContentTitle(title)
.setDefaults(Notification.DEFAULT_SOUND)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify((int)value, mBuilder.build());
However they display on status bar if phone screen is lock by sliding. I want to show the push notifications on top of screen like whatsapp, facebook, sms messages etc.
How can I make this possible?
You can do this way:
By adding below line:
setPriority(Notification.PRIORITY_MAX)
Full code:
mNotificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
i, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setContentTitle(title)
.setDefaults(Notification.DEFAULT_SOUND)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg)
.setPriority(Notification.PRIORITY_MAX);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify((int)value, mBuilder.build());
Hope this will help you.
I am trying to write a simple notification code in a button that create a notification when i press on it but the notification doesn't appear when i press on the button here is the code i am using
int notificationId = 001;
// Build intent for notification content
Intent viewIntent = new Intent(SessionsActivity.this, MainActivity.class);
PendingIntent viewPendingIntent =
PendingIntent.getActivity(SessionsActivity.this, 0, viewIntent, 0);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(SessionsActivity.this)
.setContentTitle("hii")
.setContentText("hii2")
.setContentIntent(viewPendingIntent);
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =NotificationManagerCompat.from(SessionsActivity.this);
// Build the notification and issues it with notification manager.
notificationManager.notify(notificationId, notificationBuilder.build());
Intent viewIntent = new Intent(SessionsActivity.this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, viewIntent , PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getResources().getString(R.string.app_name))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
notificationBuilder.setStyle(inboxStyle);
inboxStyle.setBigContentTitle("sdjshfjnds");
inboxStyle.addLine("sdjjsdfn");
notificationBuilder.setStyle(inboxStyle);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int NOTIFICATION_ID = 100;
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());