I literally have tried everything seriously every method and every snippet but still, I was not able to show heads-up notifications on Chinese brand devices.
so yesterday I thought why not try it again but after all again still I'm not able to show heads-up notification until I manually goto the app into the settings and give floating permission for the app.
Now most of you may say why not to navigate the user to the setting when he/she first opens up the app but nobody likes that even there are other apps (I'm not talking about white list app like WhatsApp) which have 10K downloads are able to show heads up notification
Here is my code, btw I have tried setting the sound, vibration, and light but still heads up are not showing, and yes I do uninstall my app after every build
public void showNotification(View v){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel nc = new NotificationChannel("n","pop up notification", NotificationManager.IMPORTANCE_HIGH);
nc.enableLights(true);
nc.setLightColor(Color.BLUE);
nc.enableVibration(true);
nc.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager nm = getSystemService(NotificationManager.class);
nm.createNotificationChannel(nc);
}
Notification.Builder notification = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
notification = new Notification.Builder(this,"n")
.setContentTitle("Pop up notification")
.setSmallIcon(R.drawable.ic_launcher_background);
}else{
notification = new Notification.Builder(this)
.setContentTitle("Pop up notification")
// .setPriority(Notification.PRIORITY_MAX)
.setSmallIcon(R.drawable.ic_launcher_background);
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1,notification.build());
}
I have two solutions. Try them out and see if any works.
Yours actually works Like this
public void showNotification(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel nc = new NotificationChannel("n","pop up notification", NotificationManager.IMPORTANCE_HIGH);
nc.enableLights(true);
nc.setLightColor(Color.BLUE);
nc.enableVibration(true);
nc.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager nm = getSystemService(NotificationManager.class);
nm.createNotificationChannel(nc);
}
Notification.Builder notification = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
notification = new Notification.Builder(this,"n")
.setContentTitle("Pop up notification")
.setSmallIcon(R.drawable.alex);
}else{
notification = new Notification.Builder(this)
.setContentTitle("Pop up notification")
//.setPriority(Notification.PRIORITY_MAX)
.setSmallIcon(R.drawable.alex);
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1,notification.build());
Mine notifyUser("Order Delivery", "Your order has arrived"):
public void notifyUser(String title, String text){
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "notify_001");
Intent intent = new Intent(this, ActivityToOpenWhenNotificationIsTapped.class);
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.setBigContentTitle(title);
bigTextStyle.bigText(text);
bigTextStyle.setSummaryText("Big summary text. Comment if you want.");
builder.setAutoCancel(true);
builder.setContentIntent(activity);
builder.setContentTitle(title);
builder.setContentText(text);
builder.setPriority(1);
builder.setSmallIcon(R.drawable.alex);
builder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.alex));
builder.setStyle(bigTextStyle);
builder.setAutoCancel(true);
builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
builder.setGroup("Pixels");
Notification build = builder.build();
build.flags |= 16;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= 26) {
String str3 = "Pixels_V1.0";
notificationManager.createNotificationChannel(new NotificationChannel(str3, "Pixels Notification", NotificationManager.IMPORTANCE_DEFAULT));
builder.setChannelId(str3);
}
int NOTIFICATION_ID = new Random(System.currentTimeMillis()).nextInt(1000);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
Related
in my application, i have created 3 notification channels (Follow, Comment, Like) to receive notifications from Firebase Cloud Messaging.
When i run app and go to Settings > Apps > My App > App Notifications , I found it show only one notification channel (ex. Follow channel) and other two channels are't be shown ...
But when i receive notification from Comment channel for example, it updates current channel with Comment channel and when i receive notification from Like channel, it updates current channel with Like channel and so on..
so why it doesn't show three channels as separated channels at same time ?!
Here is Code I work with:
public class MyFireBaseMessagingService extends FirebaseMessagingService {
String title, content, type;
#Override
public void onMessageReceived(#NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
title = remoteMessage.getData().get("Notif_Title");
content= remoteMessage.getData().get("Notif_Content");
type = remoteMessage.getData().get("Type");
if (type.equals("Follow")) {
NotificationManager manager1 = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent result = new Intent(this, MainActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, result, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(title)
.setContentText(postContent)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(resultPendingIntent)
//to work on android 8 and high..
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId("YOUR_PACKAGE_NAME");
builder.setSmallIcon(R.drawable.logo);
NotificationChannel channel = new NotificationChannel(
"YOUR_PACKAGE_NAME",
"Follow",
NotificationManager.IMPORTANCE_HIGH);
manager1.createNotificationChannel(channel);
}
manager1.notify(0, builder.build());
} else if (type.equals("Comment")) {
NotificationManager manager2 = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent result = new Intent(this, MainActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 1, result, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(title)
.setContentText(content)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(resultPendingIntent)
//to work on android 8 and high..
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId("YOUR_PACKAGE_NAME");
builder.setSmallIcon(R.drawable.logo);
NotificationChannel channel = new NotificationChannel(
"YOUR_PACKAGE_NAME",
"Comment",
NotificationManager.IMPORTANCE_HIGH);
manager3.createNotificationChannel(channel);
}
manager3.notify(1, builder.build());
} else if (type.equals("Like")) {
Intent result = new Intent(this, MainActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 2, result, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager manager3 = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(title))
.setContentText(content)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(resultPendingIntent)
//to work on android 8 and high..
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId("YOUR_PACKAGE_NAME");
builder.setSmallIcon(R.drawable.logo);
NotificationChannel channel = new NotificationChannel(
"YOUR_PACKAGE_NAME",
"Like",
NotificationManager.IMPORTANCE_HIGH);
manager3.createNotificationChannel(channel);
}
manager3.notify(2, builder.build());
}
}
}
Can Anyone help me...
I know how to solve it ...
You should change this code
builder.setChannelId("YOUR_PACKAGE_NAME");
builder.setSmallIcon(R.drawable.logo);
Into specific channel id to each one like :
builder.setChannelId("com.myapp.first_notification");
builder.setSmallIcon(R.drawable.logo);
So I am a beginner and I was following one tutorial on how to make notification in Android Studio. This app should make a notification when you press a button, but instead of making notification it shows up a Developer Warning
Error:
No Channel found for pkg=com.example.myapplication, channelId=My notification, id=1, tag=null, opPkg=com.example.myapplication, callingUid=10153, userId=0, incomingUserId=0, notificationUid=10153, notification=Notification(channel=My notification shortcut=null contentView=null vibrate=null sound=null defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE)
Code(Java):
public void onClick(View v) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this,"My notification");
builder.setContentTitle("My Title");
builder.setContentText("Test");
builder.setSmallIcon(R.drawable.ic_launcher_background);
builder.setAutoCancel(true);
NotificationManagerCompat managerCompat = NotificationManagerCompat.from(MainActivity.this);
managerCompat.notify(null,0, builder.build());
}
});
Do you know how to fix this?
Starting with Android 8.0 (API level 26), notifications require a notification channel.
This is a category which you create and assign to your notifications. Having multiple notification channels per app, allows users to better manage which kind of notifications they want to receive from an app.
To achieve this, you need to create a notification channel as stated in the example in the Android docs:
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
Source
CHANNEL_ID can be any integer number which doesn't change during the lifetime of your app. It is OK if you make it a static final int.
Once you created the channel, you can specify the same CHANNEL_ID when creating notifications:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(textTitle)
.setContentText(textContent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
Source
private Notification getNotification() {
Intent notificationIntent = new Intent(this, VideoExportActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationChannel notificationChannel = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationChannel = new NotificationChannel("CHANNEL_ID", "Alarm Time....", NotificationManager.IMPORTANCE_DEFAULT);
}
NotificationManager notificationManager = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
notificationManager = getSystemService(NotificationManager.class);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager.createNotificationChannel(notificationChannel);
}
return new NotificationCompat.Builder(this, "CHANNEL_ID")
.setContentTitle("Title")
.setContentText("Your Message")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentIntent(pendingIntent)
.build();
}
Notification notification = getNotification();
When creating a notification in android Oreo everything works until I use .setStyle then it stops working
Code:
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int importance = NotificationManager.IMPORTANCE_HIGH;
{
NotificationChannel mChannel = new NotificationChannel(
CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, importance);
assert notificationManager != null;
notificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setContentTitle(track.getTitle())
.setContentText(track.getArtist())
.setSmallIcon(R.drawable.icon)
.setOnlyAlertOnce(true)//show notification for only first time
.setLights(Color.CYAN, 500, 1200) //Setting the color of the led blink on the phone for notifications
.addAction(drw_previous, "Previous", pendingIntentPrevious) //Setting action for the previous button
.addAction(playbutton, "Play", pendingIntentPlay) //Setting the action for the play/pause button
.addAction(drw_next, "Next", pendingIntentNext) //Setting the action for he next button
.setDeleteIntent(pendingIntentDelete) //Setting delete intent for when the notification is destroyed/deleted
.setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0, 1, 2)
.setMediaSession(mediaSessionCompat.getSessionToken())) //Setting how and what order the buttons will be shown
.setShowWhen(false) //Do not show time for when this notification was created
.setDefaults(Notification.DEFAULT_ALL); //Defaults
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
notificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());
}
Am I missing something. I have everything that should make it work. It used to work, but I am revisiting code changing it and making it better and not sure what went wrong this time. Your help is appreciated!
I have the same issue as this person: NotificationManager Error Android Studio
However I already have the solution implemented a while ago but this just stopped working. I am at build API 28 on a P10 with Oreo 8.0.0 installed. My code executes the notification logic, but I get this error:
E/NotificationManager: notifyAsUser: tag=null, id=-1522221101, user=UserHandle{0}
and even though the icon for the notification pops up in the phone's notification bar, and tapping it opens my intent, the user gets no notification that this message arrived. I tried with the app in the foreground and in the background - both cases the small icon appears in the notification bar on top of the phone's UI, but no sound and no message.
Here is my code:
Intent intent = new Intent(getContext(), AuthRequestActivity.class);
intent.putExtra(getContext().getPackageName().concat(".param0"), code);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel("default", "My Channel", NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("ALP Push Notifications");
notificationManager.createNotificationChannel(channel);
}
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getContext(), "default").setSmallIcon(R.drawable.app_icon)
.setContentTitle("Authentication Request").setContentText(code)
.setAutoCancel(true).setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getContext());
// Below alternative makes no difference
// NotificationManager notificationManager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) System.currentTimeMillis(), notificationBuilder.build());
What am I doing wrong?
I'm trying to show notification but it's not working for both Oreo and Pie version.
It's working in kitkat version.
I tried all the possible condition i can do, i don't know what else i'm missing here
Here is my code:
String idChannel = "my_channel_01";
Intent mainIntent;
mainIntent = new Intent(ChecklistOptionActivity.this, CashCountOptionActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, mainIntent, 0);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel mChannel = null;
// The id of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentTitle(context.getString(R.string.app_name))
.setSmallIcon(R.drawable.bc_icon)
.setContentIntent(pendingIntent)
.setContentText("Test");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mChannel = new NotificationChannel(idChannel, context.getString(R.string.app_name), importance);
// Configure the notification channel.
mChannel.setDescription("Test");
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mNotificationManager.createNotificationChannel(mChannel);
} else {
builder.setContentTitle(context.getString(R.string.app_name))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setColor(ContextCompat.getColor(context, R.color.colorBlue))
.setVibrate(new long[]{100, 250})
.setLights(Color.YELLOW, 500, 5000)
.setAutoCancel(true);
}
mNotificationManager.notify(1, builder.build());
String channel_id = createNotificationChannel(context);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, channel_id);
The below method is to generate a new channel_id.
public static String createNotificationChannel(Context context) {
// NotificationChannels are required for Notifications on O (API 26) and above.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// The id of the channel.
String channelId = "Channel_id";
// The user-visible name of the channel.
CharSequence channelName = "Application_name";
// The user-visible description of the channel.
String channelDescription = "Application_name Alert";
int channelImportance = NotificationManager.IMPORTANCE_DEFAULT;
boolean channelEnableVibrate = true;
// int channelLockscreenVisibility = Notification.;
// Initializes NotificationChannel.
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, channelImportance);
notificationChannel.setDescription(channelDescription);
notificationChannel.enableVibration(channelEnableVibrate);
// notificationChannel.setLockscreenVisibility(channelLockscreenVisibility);
// Adds NotificationChannel to system. Attempting to create an existing notification
// channel with its original values performs no operation, so it's safe to perform the
// below sequence.
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
assert notificationManager != null;
notificationManager.createNotificationChannel(notificationChannel);
return channelId;
} else {
// Returns null for pre-O (26) devices.
return null;
}
}
Here, you will get push notification using channel_id in your device which is consist 26+ SDK version.
Because, the NotificationCompat.Builder(context) method has been deprecated so that you will have to use an updated method NotificationCompat.Builder(context, channel_id) which has two parameters one is for context, the second one is for channel_id.
In the devices which has 26+ SDK version, you can create channel_id every time.