I am trying to implement an android TV application by using android studio and TV emulator.
I call the following code blocks at the onCreate function of the main activity. It is running for android mobile devices but not working on the Android TV emulator.
String channel_name = "myChannel";
String channel_description = "mySChannel";
String CHANNEL_ID = "idididf";
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_my_icon)
.setContentTitle("My notification")
.setContentText("Much longer text that cannot fit one line...")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("Much longer text that cannot fit one line..."))
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = channel_name;
String description = 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.createNotificationChannel(channel);
}
notificationManager.notify(12312, builder.build()); // 0 is the request code, it should be unique id
Any idea?
With very few exceptions, notifications are not shown on Android TV.
Related
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();
I want to show notifications on lock screen, but I'm unable to get it to work.
This is my code that I have;
.setSmallIcon(R.drawable.deleteaccounticon)
.setContentTitle("My notification")
.setContentText("Much longer text that cannot fit one line...")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("Much longer text that cannot fit one line..."))
.setPriority(NotificationCompat.PRIORITY_HIGH);
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "channel1";
String description = "test";
int importance = NotificationManager.IMPORTANCE_HIGH;
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 = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
notificationManager.notify(1,builder.build());
channel.setLockscreenVisibility(VISIBILITY_PUBLIC);
}
The notifications is not showing on the lock screen. The settings for the app allows to show notifications on lock screen.
Any advice?
builder.setOngoing(true)
makes the notification shown on the lock screen.
I have a simple notification which works for me on older androids. But this same notification doesnt pop up on my phone with android oreo. I read that i need to assign channel with notification, which i did. But still, it doesnt pop up.
Thanks
My code -
String CHANNEL_ID = "my_channel_01";
String name = "notification";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
NotificationManager mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(this,CHANNEL_ID)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
.setChannelId(CHANNEL_ID)
.build();
I think you are missing mManager.createNotificationChannel(mChannel);
I'm new to Android programming and I'm trying to build my first app. Right now I want to send local notifications. Unfortunately I cannot receive notifications on devices running API 28 and above. I know there is a change in the way notifications are sent since Oreo and I have included code that creates the channel. It seems like if I run the app on a simulator with a lower API (e.g. 19) the notification is received. Also if I copy my code into a new project I receive the notifications, even on a simulator running Android Oreo. What settings in the project could cause notifications not to be received on Android Oreo? (There are no errors in Logcat)
My code for sending the notifications:
public static final String CHANNEL_1_ID = "channel1";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createNotificationChannels();
}
public void setNotif(View view) {
Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("hey")
.setContentText("world")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(1, notification);
}
private void createNotificationChannels() {
// if higher than android oreo
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel1 = new NotificationChannel(CHANNEL_1_ID, "Channel 1", NotificationManager.IMPORTANCE_HIGH);
channel1.setDescription("This is channel 1");
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel1);
}
}
The setNotif method is called by a tap of a button. Again, I'm new to Android programming so any advice would be helpful. Maybe even a different way in which I could diagnose the issue. Thank you!
Add this check in your setNotif() method:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
notificationManager.createNotificationChannel(getNotificationChannel(context));
setNotif() method will be changed like this :
public void setNotif(View view) {
Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("hey")
.setContentText("world")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) notificationManager.createNotificationChannel(getNotificationChannel(context));
notificationManager.notify(1, notification);
}
And getNotificationChannel() method will look like this:
private static NotificationChannel getNotificationChannel(Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
return null;
CharSequence name = context.getString(R.string.app_name);// The user-visible name of the channel.
int importance = android.app.NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(BuildConfig.APPLICATION_ID, name, importance);
notificationChannel.setShowBadge(true);
return notificationChannel;
}
On Oreo (API 18), I don't want to use notification dot.
But it show notification dot default.
For instance, YouTube push notification but don't use notification dot.
I'am using NotificationChannel
And I tried using
NotificationChannel.setShowBadge(false)
But it didn't work.
How can I do this?
youtube - has no notification dot
myApp - has notification dot
When you create your notification under Oreo you must create a channel instance and assign it to the notification. You use setShowBadge on your instance.
Below is my code which correctly removes the badge.
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
String CHANNEL_ID = "my_channel";
CharSequence name = context.getString(R.string.channel_name);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_LOW);
channel.setShowBadge(false);
notificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context, CHANNEL_ID).setSmallIcon(iconID)
.setContentTitle(task.taskTitle)
.setContentText(task.taskNote).setOngoing(true).setWhen(0)
.setChannelId(CHANNEL_ID)
.setSound(null)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
notificationManager.notify(task.driveId, NOTIFICATION_ID, builder.build());