Cannot find symbol method setVisibility(int) - android

I am trying to build a notification method that would result in a notification appearing on the locked screen when a particular beacon is detected. My understanding is that I need to include .setVisibility(0) in the following code:
public void showNotification(Beacon beacon) {
Resources r = getResources();
int random = (int)System.currentTimeMillis();
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(android.R.drawable.ic_popup_reminder)
.setContentTitle("Beacons Found")
.setContentText(beacon.getID().toString())
.setVisibility(0) // allow notification to appear on locked screen
.setAutoCancel(true)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(random, notification);
}
I have the above code, but when I run it it says "Cannot find symbol method setVisibility(int)". I have done some research online and it seems I need to import these:
import android.support.v4.app.NotificationCompat;
import android.app.Notification;
import android.app.NotificationManager;
Even if I include these imports, I still get the same error message. Can anyone help? Thanks!!

setVisibility() was added in Api 21 According to documentation I found may try this.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(android.R.drawable.ic_popup_reminder)
.setContentTitle("Beacons Found")
.setContentText(beacon.getID().toString())
.setVisibility(0) // allow notification to appear on locked screen
.setAutoCancel(true)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(random, notification);
}

Related

Xamarin how can i get all system notification?

I'm new here. I'm developing an app that is using google maps when i press a FAT button. As at the moment google maps does not indicate when you finished a Navigation and so to be able to return to my application, I am trying to do this in some way.
I was thinking, and google maps when you're in navigation mode, it shows you a notification. Until the navigation is finished, it is not deleted. I wanted to get this notification and when my button was pressed, check if the notification is active. In case it is not active, then I can return to my application.
This is what i've been trying to do:
NotificationManager notificationManager =
GetSystemService(Context.NotificationService) as NotificationManager;
StatusBarNotification[] nnn2 = notificationManager.GetActiveNotifications();
When i check nnn2 to see what services are active, it has nothing. I don't know if i need to add some permissions in manifest, or i'm totally wrong.
This is possible and can be done something like this:
For levels below API 11 :
Notification notify = new Notification();
notify.Defaults = NotificationDefaults.Sound;
notify.Defaults = NotificationDefaults.Vibrate;
notify.Flags = NotificationFlags.NoClear;// Cruicial line of code
For API 11 and above:
Notification notify = new Notification();
notify.Defaults = NotificationDefaults.Sound;
notify.Defaults = NotificationDefaults.Vibrate;
notify.Flags = NotificationFlags.NoClear;
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.SetSmallIcon(Resource.Drawable.Icon)
.SetContentTitle(messageTitle)
.SetContentText(messageBody)
.SetOngoing(true)//Important line of code
.SetContentIntent(pendingIntent);
NotificationManagerCompat notificationManager = NotificationManagerCompat.From(this);
notificationManager.Notify(0, notificationBuilder.Build());

Creating a notification with notification channel gives no effect

I've been browsing the net for an answer but i can't find it myself despite reading the developer guides on the official android documentation site and code snippets from all over the Internet.
I've been practicing creating notifications on a new clean project, and despite creating a notification channel and the notification itself I still can't get it to run and get a message saying: "Failed to post notification on null channel". But im creating the channel, and assigning it to my notification just like it says everywhere. Im completely out of ideas and honestly i've been browsing stack for at least few hours now and every answer looks the same but it really didn't help me at all, so maybe if someone who's better than a begginer looks at my code can tell me what's wrong with my code? It's annoying because notifications are an important part of a project im working on, and it stopped me in my tracks.
public class MainActivity extends AppCompatActivity {
NotificationCompat.Builder notification;
private static final int NOTIFICATION_ID = 1;
private static final String NOTIFICATION_CHANNEL_ID = "my_notification_channel";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notification = new NotificationCompat.Builder(this);
notification.setAutoCancel(true);
findViewById(R.id.buckysButton).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
createNotification(view);
}
});
}
public void createNotification(View view){
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
nm.createNotificationChannel(notificationChannel);
}
notification.setSmallIcon(R.drawable.ic_priority_high_black_24px);
notification.setTicker("Ticker");
notification.setWhen(System.currentTimeMillis());
notification.setContentTitle("Title");
notification.setContentText("Body Body Body Body Body Body Body Body Body ");
notification.setChannel(NOTIFICATION_CHANNEL_ID);
notification.build();
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
nm.notify(NOTIFICATION_ID, notification.build());
}}
By the way I'm developing for Android minimum level of 5.1 and I have been wondering if it's really necessary to put channels in my application, but I did it anyways since when im trying to do it without them I'm still getting exactly the same error.
EDIT: Im still struggling with this issue. Do I have to do something in the gradle files to be able to pass two arguments into the NotificationCompat.Builder constructor? It wont let me do it no matter what i do, i think thats the main problem here.
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
Doesnt do it for me, it still doesnt want to take the channel_id as an argument.
new NotificationCompat.Builder(this);
is deprecated, try using
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
Here's a sample code, create a NotificationHelper class as follows,
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
public class NotificationHelper extends ContextWrapper {
private NotificationManager mNotificationManager;
private final String MY_CHANNEL = "my_channel";
private final long[] vibrationScheme = new long[]{200, 400};
/**
* Registers notification channels, which can be used later by individual notifications.
*
* #param context The application context
*/
public NotificationHelper(Context context) {
super(context);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
// Create the channel object with the unique ID MY_CHANNEL
NotificationChannel myChannel =
new NotificationChannel(
MY_CHANNEL,
getResources().getString(R.string.notification_channel_title),
NotificationManager.IMPORTANCE_DEFAULT);
// Configure the channel's initial settings
myChannel.setLightColor(Color.GREEN);
myChannel.setVibrationPattern(vibrationScheme);
// Submit the notification channel object to the notification manager
getNotificationManager().createNotificationChannel(myChannel);
}
}
/**
* Build you notification with desired configurations
*
*/
public NotificationCompat.Builder getNotificationBuilder(String title, String body, Intent pendingIntent) {
Bitmap notificationLargeIconBitmap = BitmapFactory.decodeResource(
getApplicationContext().getResources(),
R.mipmap.ic_launcher);
return new NotificationCompat.Builder(getApplicationContext(), MY_CHANNEL)
.setSmallIcon(R.drawable.ic_notification_icon)
.setLargeIcon(notificationLargeIconBitmap)
.setContentTitle(title)
.setContentText(body)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setVibrate(vibrationScheme)
.setStyle(new NotificationCompat.BigTextStyle().bigText(body))
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
.setContentIntent(pendingIntent)
.setAutoCancel(true);
}
public NotificationManager getNotificationManager() {
if (mNotificationManager == null) {
mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return mNotificationManager;
}
}
and then use it as follows,
NotificationHelper notificationHelper = new NotificationHelper(context);
NotificationCompat.Builder builder = notificationHelper.getNotificationBuilder(title, message);
notificationHelper.getNotificationManager().notify(notificationID, builder.build());
Ok, maybe I'm bit late, but...
Try to shorten your channelId string.
Now it
private static final String NOTIFICATION_CHANNEL_ID = "my_notification_channel";
Try
private static final String NOTIFICATION_CHANNEL_ID = "my_channel";
For me it was a very strange bug. When channelId was too long it don't show notification on my device.
Edit:
It was not too long or too short, just some string got banned in system. Also it not showed in Android notification settings screen.
I've found out what was missing. I was using android studio 2.3.3 instead of the newest 3.0.1 so I couldn't import some of the new functions through Gradle.
After that i only had to change my dependencies classpath to 'com.android.tools.build:gradle:3.0.1' and the problem was gone.
Thanks for help.

Android appcompat v7 support library Notification compact view repeating buttons issue

This is the method I use to retrieve/create/update app's notification inside a service class, called PlayerService:
import android.support.v7.app.NotificationCompat;
import android.app.Notification;
import android.app.NotificationManager;
// ...
private Notification getCompatNotification(String contentText) {
m_notificationBuilder
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("PlayerService")
.setContentText(contentText)
.setContentIntent(null)
.setWhen(0)
.setShowWhen(false)
.addAction(android.R.drawable.ic_media_previous, "", null)
.addAction((isPlaying() ? android.R.drawable.ic_media_pause : android.R.drawable.ic_media_play), "", null)
.addAction(android.R.drawable.ic_media_next, "", null)
.setStyle(new NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0, 1, 2)
.setShowCancelButton(true)
.setCancelButtonIntent(null))
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setPriority(NotificationCompat.PRIORITY_MAX);
Notification notification = m_notificationBuilder.build();
return notification;
}
Now, when the media player activity is opened but playback not started, notification shows its big view with those 3 action buttons(previous, play/pause, next), but when the playback is started, notification view changes to compact and displays those 3 buttons in first place and then again the first and second. Please see the images.Test device has KitKat 4.4.4.
No Playback
Playback started
To update notification:
private void updateNotification(String contentText){
nm.notify(NOTIFICATION_ID, getCompatNotification(contentText));
}
And in onCreate():
#Override
public void onCreate() {
super.onCreate();
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
m_notificationBuilder = new NotificationCompat.Builder(this);
}
In onStartCommand():
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
startForeground(NOTIFICATION_ID, getCompatNotification("Test Content"));
return START_STICKY; // run until explicitly stopped.
}
Can't figure out what is the problem. Any help highly appreciated.
You are using the same NotificationCompat.Builder instance to create your Notifications and whenever you add an Action it is added to the previous List and creates an unwanted behavior into the System Api. You have to call clearActions() first on the Builder before adding any if you want to use the same Builder instance.
hope this could help: add android:stateListAnimator="#null" to your button

Notification notify() wrong arguments

I building a notifiaction with NotificationCompat.Builder and to show it I need to notify NotificationManager about new notifation, so I'm calling
NotificationManager mNotifyMgr = (NotificationManager) cont.getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyMgr.notify(SOME_INT_NUMBER, builder.build());
but eclipse marks "notif(..)" as error with caption:
The method notify() in the type Object is not applicable for the arguments (int, Notification)
And I'm pretty sure notif(int, Notification) exists:
http://developer.android.com/reference/android/app/NotificationManager.html
Can somebody explain what I'm doing wrong?
EDIT:
I discovered also I can't import android.app.NotificationManager, due to:
The import android.app.NotificationManager conflicts with a type defined in the same file
I am using the Notification Compat Builder and Notification Manager to generate notifications and it works just fine for me. Pasting the working code below, check if you have missed out something:
import android.app.NotificationManager;
import android.support.v4.app.NotificationCompat;
final int notificationID = (int)System.currentTimeMillis();
final int icon = R.drawable.nf_notification;
final NotificationManager notificationManager = (NotificationManager)context
.getSystemService(Context.NOTIFICATION_SERVICE);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setSmallIcon(icon)
.setContentTitle(title).setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentIntent(intent).setAutoCancel(true).setContentText(message);
notificationManager.notify(notificationID, builder.build());

Why does Eclipse not recognize the build() method from the Notification.Builder class to return a Notification object?

This is my code:
NotificationManager mNotificationManager = (NotificationManager) c.getSystemService(ns);
//Instantiate the notification
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();
Notification.Builder builder = new Notification.Builder(c)
.setTicker(tickerText)
.setWhen(when)
.setContentTitle("Test Notification")
.setContentText(arg1.getStringExtra("info"))
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true);
Notification notification = builder.getNotification();
mNotificationManager.notify(88, notification);
It works find, but using Notification notification = builder.getNotification(); is deprecated. as I should be doing Notification notification = builder.build();.
Problem is Eclipse isn't recognizing it as such, meaning it won't let me compile. The doc is clear that build() exists and its the preferred method, but its not working on my end. I would like to use non-deprecated code, so any help will be much appreciated.
imports
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
Be aware that import android.app.Notification.Builder; is saying its not being used.
If you want develop for version SDK lower than 11, you can use this code instead android.app.Notification.Builder class for creation notification:
private void createNotification(){
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "Hello", System.currentTimeMillis());
Intent notificationIntent = new Intent(this, YourActivity.class);
Random r = new Random();
int notificationId = r.nextInt();
notificationIntent.putExtra("n_id", notificationId);
PendingIntent contentIntent = PendingIntent.getActivity(this, notificationId, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.setLatestEventInfo(this, "Party", "Welcome!", contentIntent);
mNotificationManager.notify(notificationId, notification);
}
You can cancel this notification in YourActivity like this:
public class YourActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity);
int notificationId = getIntent().getIntExtra("n_id", -1);
if (notificationId!=-1) {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(notificationId);
}
}
}
This only happens when, You are compiling your app code against the version which does not hold required method/api (in your case it is build()).
If build is not available in the version you are compiling with, I will suggest you to compile with higher version of Android, You always have minimum sdk version in manifest for backward compatibility.
Thanks

Categories

Resources