I have a notification set up :
private void sendNotificationForCancellation(String appointmentId, String title, String message) {
//AppController.getSharedPreferences().edit().putString(Constants.CALL_CASE_ID, notifObject.getCaseDetails().getCaseID()).commit();
Intent intent = new Intent(this, ActCallRequestsDetail.class);
intent.putExtra("appointment_id", appointmentId);
intent.putExtra("isAppIdAvailable", true);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_note_plus)
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.app_icon))
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Random generator = new Random();
notificationManager.notify(generator.nextInt(), notificationBuilder.build());
}
On click of notification it opens a activity
Scenario-1:
It is working fine when I have a single notification.
.
Scenario-2:
Assume 2 notifications generated from above notification setup first
notification click, it is opening the activity Now i am there in the
opened activity(ActCallRequestsDetail.class) When I click the
second notification, nothing happens the activity
(ActCallRequestsDetail.class) is not refreshed
.
How to solve the Scenario-2
Solution was just adding a new ID for PendingIntent reference
private void sendNotificationForCancellation(String appointmentId, String title, String message) {
Random generator = new Random();
//AppController.getSharedPreferences().edit().putString(Constants.CALL_CASE_ID, notifObject.getCaseDetails().getCaseID()).commit();
Intent intent = new Intent(this, ActCallRequestsDetail.class);
intent.putExtra("appointment_id", appointmentId);
intent.putExtra("isAppIdAvailable", true);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, generator.nextInt(), intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_note_plus)
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.app_icon))
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(generator.nextInt(), notificationBuilder.build());
}
Related
I get a push notification from FCM. When my application is minimized or screen is locked, I see a title of notification : Here is your notification and different icon.
How I can change this? This is my code and how I create and display a notification I don't have this:
private void createNotification( String messageBody) {
Intent intent = new Intent( this , NotificationActivity. class );
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultIntent = PendingIntent.getActivity( this , 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder( this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Wiadomosc z serwera")
.setContentText(messageBody)
.setAutoCancel( true )
.setSound(notificationSoundURI)
.setContentIntent(resultIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mNotificationBuilder.build());
}
Modified createNotification method .
1 . Add setLargeIcon() method to set large icon .
2 . Put your own title in setTitle() method .
private void createNotification( String messageBody) {
Intent intent = new Intent( this , NotificationActivity. class );
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultIntent = PendingIntent.getActivity( this , 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder( this)
.setSmallIcon(R.mipmap.ic_launcher) /*Icon to display in the Notification Bar */
.setLargeIcon(R.mipmap.largeicon) /*Icon to display when you scroll down notification tray */
.setContentTitle("Your own Title") /*Notification Title which will display when you scroll down Notification tray */
.setContentText(messageBody)
.setAutoCancel( true )
.setSound(notificationSoundURI)
.setContentIntent(resultIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mNotificationBuilder.build());
}
You'll have to use setLargeIcon(Bitmap bitmap) method.
Bitmap largeIconBitmap = BitmapFactory.decodeResource(this.getResources(),R.drawable.launcher_logo);
NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder( this)
.setLargeIcon(largeIconBitmap)
.setSmallIcon(R.mipmap.ic_launcher)
I have to send a URL in notification and I want upon clicking the notification, the link will be opened immediately by Chrome. Currently, when I click the notification, it only opens my app.
What changes do I have to implement in this code for my desired behavior?
private void sendNotification(String messageBody, String title) {
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);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
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);
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 ?
I want it to create a pendingIntent notification after clicking on a button on the screen that says 'notify'. So when the user clicks on the notification it will dial a number like 021-1234567. How do I do this?
I have been searching online for help but I can't seem to find anything relating to this.
public void notifyPendingIntent(View view) {
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+ 0211234567));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
// Build notification
Notification notify = new Notification.Builder(this)
.setContentTitle("Make this call")
.setContentText("New call must be made to 021 123 4567")
.setTicker("New Alert")
.setContentIntent(pIntent).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// hide the notification after its selected
notify.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notify);
}
I have this so far, but when I hit the button, nothing happens.
I tried this and it now works. Thanks to everyone who tried to help.
public void notifyPendingIntent(View view) {
NotificationCompat.Builder myNotification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.my_notification_icon)
.setContentText("Calling 021-12345678")
.setContentTitle("Phone Call Notification");
Intent phoneCall = new Intent(Intent.ACTION_CALL);
phoneCall.setData(Uri.parse("tel:021-12345678"));
PendingIntent phoneCallIntent = PendingIntent.getActivity(this, 0, phoneCall, PendingIntent.FLAG_UPDATE_CURRENT);
myNotification.setContentIntent(phoneCallIntent);
NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, myNotification.build());
}
<!-- NOTE! Your uses-permission must be outside the "application" tag
but within the "manifest" tag. -->
<uses-permission android:name="android.permission.CALL_PHONE" />
Try this
public void call() {
Toast.makeText(this,"call",Toast.LENGTH_SHORT).show();
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel: 0211234567"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, callIntent, 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());
}