I've read most question/answers on this but my notification still isn't showing up.
My Broadcast receiver gets called, but then nothing happens. There is no error message nor exception. here is my code:
public class AlarmReceiver extends BroadcastReceiver {
private static int notificationId = 0;
#Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 101, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context, "mychannel")
.setSmallIcon(R.drawable.icon_awake)
.setContentTitle("MyAlarm")
.setContentText("Something")
.setAutoCancel(false)
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent);
notificationManager.notify(notificationId++, mNotifyBuilder.build());
}
}
I think you forget to Read Google Documents on Oreo 8.0.
If you want to create a notification on 8.0 you need to use NotificationChannel
like this:
NotificationChannel channel = null;
String chanel = "MY_Musixbox";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_LOW;
channel = new NotificationChannel(chanel, "MusicBox", importance);
notificationManager.createNotificationChannel(channel);
}
And then add your notification
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context, "mychannel")
.setSmallIcon(R.drawable.icon_awake)
.setContentTitle("MyAlarm")
.setContentText("Something")
.setChannelId(chanel);
.setAutoCancel(false)
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent);
notificationManager.notify(notificationId++, mNotifyBuilder.build());
Okay, so I will answer this myself in case somebody finds it: seems you need to manually create the notification channel yourself too:
if (Build.VERSION.SDK_INT >= 26) {
NotificationChannel channel = new NotificationChannel("mychannel",
"Channel name",
NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("Channel description");
notificationManager.createNotificationChannel(channel);
}
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 101, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context, "mychannel")
.setSmallIcon(R.drawable.icon_awake)
.setContentTitle("MyAlarm")
.setContentText("Something")
.setAutoCancel(false)
.setWhen(System.currentTimeMillis())
.setChannelId("default_channel")
.setContentIntent(pendingIntent);
notificationManager.notify(notificationId++, mNotifyBuilder.build());
Try this. you have to define channel id. thats compulsory in android 8.0
Related
Here is my firebase messaging service code file. My service is not called.
public class NotificationService extends FirebaseMessagingService {
String myTitle, myImage;
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
myTitle = remoteMessage.getData().get("title");
myImage = remoteMessage.getData().get("body");
Log.d("Service","Hi this is service");
//bitmap = getBitmapfromUrl(myImage);
//Toast.makeText(this,"Hi test",Toast.LENGTH_SHORT).show();
showNotification(myTitle,myImage);
}
private void showNotification(String myTitle,String myImage) {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("Notification_Title", "yes");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//Toast.makeText(this,title,Toast.LENGTH_SHORT).show();
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = "Custom_Notification";
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_notifications_none_white_24dp)
.setContentTitle(myTitle)
.setContentText(myImage)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Custom Notification",
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0, notificationBuilder.build());
}
}
If you are not received any remote message or your service is not working mean, check out the StackOverflow link.
To Create Custom Notification Channel
private void showNotification(String myTitle,String myImage) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("Notification_Title", "yes");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = "Custom_Notification";
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_notifications_none_white_24dp)
.setContentTitle(myTitle)
.setContentText(myImage)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Custom Notification",
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0, notificationBuilder.build()); }
Above code is working fine after receiving the remote message. If you want to add any custom layout then use below code
// Get the layouts to use in the custom notification
RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_small);
RemoteViews notificationLayoutExpanded = new RemoteViews(getPackageName(), R.layout.notification_large);
// Apply the layouts to the notification
Notification customNotification = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(notificationLayout)
.setCustomBigContentView(notificationLayoutExpanded)
.build();
Notification custom layout
Note: In android 10, and above Notification Service is not calling basically in the background. You will receive a notification but custom design it will not reflect. Still, If you want to receive notification in the background also. then listen to the broadcast receiver for firebase message event. that time you have to wake up your app to the foreground.
I have an calendar app where the info is downloaded online and there is a recyclerView of events and I want to notify the user when there is a calendar event for today, and notify with the event name or names. I have done some googling but haven't been able to figure this out.
In my MainActivity.java this is the method I created as a test from a page online
private void addNotification() {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.ic_check);
mBuilder.setContentTitle("Notification Alert, Click Me!");
mBuilder.setContentText("Hi, This is Android Notification Detail!");
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, mBuilder.build());
}
But when I call this method nothing is happening, what am I doing wrong?
I think you are missing the channel of the notification
I have a different solution, try this one (I checked it on my phone)
calling the function:
showSmallNotification(R.drawable.ic_launcher_background, "title","message");
Function code
private void showSmallNotification( int icon, String title, String message){
String CHANNEL_ID = "Channel_01";
String CHANNEL_NAME = "Notification";
// I removed one of the semi-colons in the next line of code
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.addLine(message);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// I would suggest that you use IMPORTANCE_DEFAULT instead of IMPORTANCE_HIGH
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
channel.enableVibration(true);
channel.setLightColor(Color.BLUE);
channel.enableLights(true);
//channel.canShowBadge();
// Did you mean to set the property to enable Show Badge?
channel.setShowBadge(true);
notificationManager.createNotificationChannel(channel);
}
Intent mainIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mainIntent, 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
.setVibrate(new long[]{0, 100})
.setPriority(Notification.PRIORITY_MAX)
.setLights(Color.BLUE, 3000, 3000)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setStyle(inboxStyle)
.setContentText(message);
// Don't forget to set the ChannelID!!
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
notificationBuilder.setChannelId(CHANNEL_ID);
}
notificationManager.notify(CHANNEL_ID, 1, notificationBuilder.build());
}
Edit
In order to check if there are any events every day you will need to use AlarmManager as from it's name you can schedule an event every day
You can see a solution (or at least it will give a direction) once you did that I think the next step would be to send the intent to a service which will check if there are any events available in current day. If there are any just show the notification/s
alarm manager link
I'm trying to create a pending URL-Intent for a Notification, that is called, if the user clicks the notification.
The problem is that the Intent is being called by creating the notification, so the url is getting called in the web browser immediatly. I want to prevent this, but do not know how?
Here is my code
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("google.com"));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(MyApp.getContext(), 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(MyApp.getContext(), "MyApp")
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("my App")
.setContentText("Click here to call the URL")
.setStyle(new NotificationCompat.BigTextStyle().bigText("...."))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
;
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(MyApp.getContext());
notificationManager.notify(getNotificationId(), builder.build());
How can I prevent executing the intent after the "notify" call??
Intent should only be executed, when the user clicks on the Notification.
try this:
public void notification()
{
Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setData(Uri.parse("https://www.google.com/"));
#SuppressLint("WrongConstant") PendingIntent pending =
PendingIntent.getActivity(this, 0, notificationIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);
String channelId = getString(R.string.app_name);
NotificationChannel notificationChannel = null;
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(getApplicationContext());
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
notificationChannel = new NotificationChannel(channelId, channelId,
NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription(channelId);
notificationChannel.setSound(null, null);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new
NotificationCompat.Builder(getApplicationContext(), channelId)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("my App")
.setContentText("Click here to call the URL")
.setStyle(new NotificationCompat.BigTextStyle().bigText("...."))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pending)
.setAutoCancel(true)
;
notificationManager.notify(0, builder.build());
}
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
intent = new Intent(context, AlertDialog.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); // added new line
intent.putExtra("STARTED_BY_RECEIVER", true);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
String CHANNEL_ID = "channel id";// The id of the channel.
CharSequence name = "Channel";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
Notification notification = new Notification.Builder(context)
.setContentIntent(pendingIntent)
.setContentText("Some Text")
.setContentTitle("Notification")
.setAutoCancel(true)
.setOngoing(true)
.setSmallIcon(R.drawable.ic_notification_praying)
.setChannelId(CHANNEL_ID)
.build();
notificationManager.createNotificationChannel(mChannel);
notificationManager.notify(100 , notification);
} else {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
intent = new Intent(context, AlertDialog.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); // added new line
intent.putExtra("STARTED_BY_RECEIVER", true);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notification_praying)
.setContentIntent(pendingIntent)
.setContentText("Some Text")
.setContentTitle("Notification")
.setAutoCancel(true)
.setOngoing(true);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);
notificationManager.notify(100, builder.build());
}
}
I've made an application which show notification on time that user chose in main activity and Notification should disappear only when user tap on notification otherwise not..and it was working fine unless I've updated my phone to android Oreo (8.0) now notification show at time but auto disappear after sometime.. How to make notification not auto disappear (in android Oreo) but only when user tap on it?
alright I figure this out.. it was battery optimize option which was killing my application so that's why notification was disappearing.. I removed my application from auto optimize now it's working fine.
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());
}