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());
Related
I have followed the Xamarin walkthrough, and it's not working for me.
The code falls through this cleanly, but it never sends the notification.
It never shows up on my emulator or device.
I have no idea what is going on.
public override void OnReceive(Context context, Intent intent)
{
string message = intent.GetStringExtra("message");
string title = intent.GetStringExtra("title");
int id = int.Parse(intent.GetStringExtra("id"));
//Generate a notification with just short text and small icon
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.SetAutoCancel(true) // Dismiss from the notif. area when clicked
.SetContentTitle(title) // Set its title
.SetContentText(message); // The message to display.
NotificationManager notificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService);
notificationManager.Notify(id, builder.Build());
Any help or links would be very helpful. I'm just completely lost; been working on this for about 14 hours now, and cannot find any help on the Google.
Answer to my inquiry: You must have an Icon set for notifications to be properly build and sent. Though, it won't send an error for not having one.
Short version: Needed to add
.SetSmallIcon(Resource.Drawable.icon);
Add an icon to notification.
Notification.Builder builder = new Notification.Builder (this)
.SetContentTitle ("Title")
.SetContentText ("Message")
.SetSmallIcon (Resource.Drawable.ic_notification);
Notification notification = builder.Build();
NotificationManager notificationManager =
GetSystemService (Context.NotificationService) as NotificationManager;
const int notificationId = 0;
notificationManager.Notify (notificationId, notification);
I want to show notifications like on picture. If there is more than one, I want to show a counter too. I didn't find info in official doc. Now I just update my notification by id:
((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE))
.notify(PUSH_NOTIFICATION_ID, notification);
How can I do it ?
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext());
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle(topic);
mBuilder.setContentText(new String(message.getPayload()));
mBuilder.setAutoCancel(true);
mBuilder.mNumber = 1;//get your number of notification from where you have save notification
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(notify_id, mBuilder.build());
To create a stack, call setGroup() for each notification you want in the stack and specify a group key.
final static String GROUP_KEY_EMAILS = "group_key_emails";
// Build the notification, setting the group appropriately
Notification notif = new NotificationCompat.Builder(mContext)
.setContentTitle("New mail from " + sender1)
.setContentText(subject1)
.setSmallIcon(R.drawable.new_mail)
.setGroup(GROUP_KEY_EMAILS)
.build();
// Issue the notification
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
notificationManager.notify(notificationId1, notif);
Reference: https://developer.android.com/training/wearables/notifications/stacks.html
I have a some troubles with understanding of goal of setGroup() method.
As docs said :
...Grouped notifications may display in a cluster or stack on devices which support such rendering....
Here it is the first question :
What it is this rendering? What's so special about it?!
I create a method which show a custom text message :
public static void showNotification(Context context, String title, String message, PendingIntent pendingIntent) {
notificationMessages.add(message);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
// .setGroupSummary(true)
.setSmallIcon(R.drawable.ic_launcher)
.setContentInfo("" + (notificationMessages.size()))
/*.setGroup(++i + "")*/;
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(title);
for (int i = 0; i < notificationMessages.size(); i++) {
inboxStyle.addLine(notificationMessages.get(i));
}
builder.setContentIntent(pendingIntent);
builder.setStyle(inboxStyle);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = builder.build();
mNotificationManager.notify(0, notification);
}
and play with notificationID, setGroup and setGroupSummary methods.
public static void showNotification(Context context, String title, String message, PendingIntent pendingIntent) {
notificationMessages.add(message);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
// .setGroupSummary(true)
.setSmallIcon(R.drawable.ic_launcher)
.setContentInfo("" + (notificationMessages.size()))
.setGroup(GROUP_KEY);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(title);
for (int i = 0; i < notificationMessages.size(); i++) {
inboxStyle.addLine(notificationMessages.get(i));
}
builder.setContentIntent(pendingIntent);
builder.setStyle(inboxStyle);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = builder.build();
mNotificationManager.notify(new Random().nextInt(3), notification);
}
But, no visual changes
comes if I commented lines or not. So here is a stuck for me in understanding of purpose of this method.
from the official docs:
http://developer.android.com/preview/features/notification-updates.html
Android N also allows you to bundle similar notifications to appear as a single notification. To make this possible, Android N uses the existing NotificationCompat.Builder.setGroup() method. Users can expand each of the notifications, and perform actions such as reply and dismiss on each of the notifications, individually from the notification shade.
Meaning the setGroup will only make a difference if the device supports it.
Devices that support it are:
Android Wear devices. when showing remote notifications, you can group together them
Android N. Devices running the Android N developer preview (or in the future the official N release), will show a group of notifications together
The following blog post shows how those work on Android N: https://medium.com/exploring-android/android-n-introducing-upgraded-notifications-d4dd98a7ca92
bellow is a render of that a group looks like:
That means that setGroup will make no difference on devices running anything bellow API23, that includes, Marshamallow, Lollipop, KitKat, etc.
setGroupSummaryis there to support grouped notifications on devices before Nougat. It replaces the single notifications with 1 summary notification.
On Nougat+ the system creates a normal group, which is not your notification with setGroupSummary(true)
I need to implement notification functionality like GMAIL i.e. if the screen is currently opened, the list should auto refresh and no notification is received. If the app is closed then we receive notification. In GMAIL if we are at Inbox and new mail arrives, its automatically appended at top without notification and if app isn't opened we get notification for new mail.
I have coded the notification part and its working fine but only thing is even if I am in current activity, I still get notifications which I am trying to stop.
Below is the code from Intent class to send notification
Intent listIntent = new Intent(this, ListActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ListActivity.class);
stackBuilder.addNextIntent(listIntent);
PendingIntent listPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("xxxx")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("Update List"))
.setContentText("Update List").setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL).setWhen(System.currentTimeMillis());
builder.setContentIntent(listPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, builder.build());
I researched a lot on web & SO but didn't get any relevant pointers. There seems to be a solution from CommonsWare but I do not want to use any external library. Also during my research I found that knowing which activity is on top isn't a good practice to do in production so can't use that too to stop notification is my current activity is on top. Please advise.
This one solved your issue
ActivityManager activityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE);
boolean isActivityFound = false;
if (services.get(0).topActivity.getPackageName().toString().equalsIgnoreCase(getPackageName().toString()))
{
isActivityFound = true;
}
if (!isActivityFound)
{
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("xxxx")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("Update List"))
.setContentText("Update List").setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL).setWhen(System.currentTimeMillis());
builder.setContentIntent(listPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, builder.build());
}
In the title of the push notifications of other apps, i can see the time in the right corner of the push. I've attached a image: the first notification has the time properly, and the second not (it is the mine haha).
This is my code:
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification;
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB)
{
// Do something for HONEYCOMB and above versions
notification = new Notification.Builder(context)
.setContentText(context.getString(R.string.app_name))
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setShowWhen(true)
.build();
} else{
// do something for phones running an SDK before HONEYCOMB
notification = new Notification(R.drawable.ic_launcher, context.getString(R.string.app_name), System.currentTimeMillis());
}
Also i am using the method setShowWhen(true).
Really, i don't know what I can do!!!