This method is inside my service ----->>>>
public class MyFirebaseMessagingService extends FirebaseMessagingService
my target activity (Event) is not opening when i m clicking on notification.
//This method is only generating push notification
//It is same as we did in earlier posts
private void sendNotification(String messageBody) {
Intent intent = new Intent(getApplicationContext(), Events.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.djchc_logo)
.setContentTitle("Notification")
.setContentText(messageBody)
//.setAutoCancel(true)
.setSound(defaultSoundUri);
//.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, 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());
}
When I send notification and app does not open already,I cant see my notification on listview in mainactivity.When I send notification and app already is open,I see on listview.How can I see notifications on listview everytime I send notification?
my service
private void sendNotification(String messageBody) {
SharedPreferences a=getSharedPreferences("noti",MODE_PRIVATE);
SharedPreferences.Editor editor=a.edit();
editor.putInt("i",a.getInt("i",0)+1);
editor.putString(String.valueOf(a.getInt("i",0)+1),messageBody);
editor.apply();
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.farmer)
.setContentTitle("Zengin Çiftçi")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
my mainactivity
a=getSharedPreferences("noti",MODE_PRIVATE);
for(int i=a.getInt("i",0);i>0;i--){
liste.add(a.getString(String.valueOf(i),""));
}
I tried to learn about Firebase. And I successfully received notification from it. But how I can open another activity when click on the notification?
This my code:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message"));
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("test")
.setContentText(remoteMessage.getData().get("message"));
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
You need to build a PendingIntent and notify using NotificationManager
Intent intent = new Intent(this, MyActivity.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.your_icon)
.setContentTitle("Notification Title")
.setContentText(messageBody)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
As Tosin suggested, if you're sending push notification from Firebase console, you need to put your payload into data key
It's sth about notification, nothing to do firebase.
check this you need to Define the Notification's Action
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());
}