I am making an extension for my android app. I can get the notification to android wear without a problem with all the data I want. I have lat and long for every POI for which im sending a notification, and i want to add a feature "Navigate to" inside android wear notification, that would take this coordinates and ran a google navigation on smart watch.
Is this possible? Can u provide some example or documentation.
Thanks
This is a sample code that i use in my app for running google maps on phone by clicking "Open on map" button on my android wear.
Intent mapIntent = new Intent(Intent.ACTION_VIEW);
Uri geoUri = Uri.parse("geo:0,0?q=" + chapter.getLatitude() + "," + chapter.getLongitude());
mapIntent.setData(geoUri);
PendingIntent mapPendingIntent =
PendingIntent.getActivity(context, 0, mapIntent, 0);
...
.addAction(R.drawable.ic_map_white_24dp, "Open on map", mapPendingIntent)
as far as I understand what you are trying to do, you already reached your goal. Once you are able to start navigation on your phone, it will be automatically displayed on your watch as a notification card which appears at the bottom of your watchface.
I'm currently developing an application where I'm doing the same thing except I'm using CardFragments for triggering the action instead of using Wearable Notifications and PendingIntents. When I press the action button of the CardFragment displayed on my watch, I'm sending a Wearable Message to my Phone App which runs the following code when it receives this particular message.
String uri = "google.navigation:q=" + String.valueOf(latitude) + "," + String.valueOf(longitude);
Intent mapsIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
mapsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mapsIntent);
I have to set the FLAG_ACTIVITY_NEW_TASK because the code is part of a method of a normal java class. Everything works fine when the screen of the phone is unlocked and screen is turned on. However, navigation should also start when the phone is locked. This is where I'm currently struggling with.
Related
I am building out a media player for Android Auto and struggling to make a simple Toast Message that appears on the Automotive Display Head Unit.
In my Custom Actions, I have an action that needs to display a toast message on the Car interface, but when I implement the toast it instead only shows on the handheld device/phone.
I have searched the internet high and low, and can not find anything about displaying toasts on the Car Head Unit, even though it is listed in the Android Auto Design guide:: https://designguidelines.withgoogle.com/android-auto/android-auto/app-ui.html#app-ui-drawer
could someone please point me to an example for giving visual feedback or toasts on the Android Auto Platform?
You cannot.
If you have a look at the following question: Develop an Android Auto custom app I have shared a jar which allows you to use some of the un-offical Android Auto SDK from there you can import this:
import com.google.android.apps.auto.sdk.CarToast;
import com.google.android.apps.auto.sdk.notification.CarNotificationExtender;
However even if you import the classes and use the CarToast like this:
CarToast.makeText(context,"SPEED CAMERA: " + text, Toast.LENGTH_LONG).show();
it will only display the toast on the phone screen not on the projected screen.
So to display a message correctly you will need to do something like this:
CarToast.makeText(context,"SPEED CAMERA: " + text, Toast.LENGTH_LONG).show();
CarNotificationExtender paramString2 = new CarNotificationExtender.Builder()
.setTitle(title)
.setSubtitle(text)
.setShouldShowAsHeadsUp(true)
.setActionIconResId(R.drawable.ic_danger_r)
.setBackgroundColor(Color.WHITE)
.setNightBackgroundColor(Color.DKGRAY)
.setThumbnail(bmp)
.build();
NotificationCompat.Builder mynot = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(text)
.setLargeIcon(bmp)
.setSmallIcon(R.drawable.ic_danger_r)
.setColor(Color.GRAY)
.extend(paramString2);
mNotifyMgr.notify(1983,mynot.build());
This will show a toast which will only be visible on the phone screen and it will show a heads-up notification which will only be visible on the car's screen. Since there is no action attached to it, nothing will happen if the use interacts with it the notification.
If the phone is connected to the car the phone's screen will be turned off anyway so displaying Toast there will be ignored.
The problem with all this is that since it's an unofficial jar and the SDK is not available to the public you won't be able to publish the app on PlayStore :(, that being said I only tried to publish complete apps, but an app which just display notification might pass through the filter.
I'm an Android app developer, and is developing an instant message app. The app has a notification problem on Android Oreo system, which shows persistent notification "App is running in the background" and cannot be cleared, and it's OK on system before Android Oreo.
Screenshot: The phone shows persistent notification App is running in the background
I find some discussion, such as Nexus Help Forum about this question, but it doesn't work in my phone's settings.
I want to know how to hide this notification programmatically and the app also can receive message instantly because it's an instant message app.
Any help is very appreciated.
The app has a notification problem on Android Oreo system, which shows persistent notification "App is running in the background" and cannot be cleared, and it's OK on system before Android Oreo.
You used startForeground() with a minimum-importance Notification.
I want to know how to hide this notification programmatically
Use startForeground() with a Notification that has higher than minimum importance. Or, do not use startForeground().
I find some install message apps such as WeChat, Facebook doesn't have this problem on Android Oreo
They are not using foreground services, presumably. For example, they might be using Firebase Cloud Messaging (FCM).
Before we talk about how to get rid of it, however, let’s talk about why it’s there in the first place.
Basically, in previous versions of Android, there was no real way of knowing if an app was running in the background doing a bunch of stuff it’s not supposed to be doing. In most scenarios, these misbehaving apps would wreak havoc on the battery by keeping the system awake—these are called “wakelocks.” In laymen’s terms, it was keeping the system from sleeping. That’s bad.
With Oreo, Google is calling out developers that let their apps do this sort of thing with the new notification. Essentially, if an app is running in the background and chewing up battery life, this new notification will tell you.
NOTE: There are a few legitimate scenarios where an app will continuously run in the background, like the VPN service running. Often, however, apps are running in the background unjustifiably.
It’s worth noting, though, removing the notification does not solve the issue. Period. There’s a reason this notification exists, and getting rid of it will do nothing to solve the underlying issue. You’ll either need to change a setting within the app or uninstall it altogether.
As long as you understand that and still want to remove it, let’s do this thing.
Because this is a relatively crucial system setting, there’s no way within Oreo itself to remove it. That makes sense.
But like with most things, the developer community has found a way to remove it, and developer iboalali released an app to do just that. It’s actually just called “Hide ‘running in the background’ Notification,” which is about as straightforward as an app name could ever be. Go ahead and give it an install.
Without root, there is no way to actually prevent Android System from displaying the persistent “app is running in the background” notification in Android 8.0 Oreo. Looking at the source code for the ForegroundServiceController, its implementation, and the ForegroundServiceDialog doesn’t really reveal anything we can take advantage of. Programatically nothing have been found so far.
Here's a Blog post that can help you
First, you must have the NotificationListenerService implementation. Second, in this service (after onListenerConnected callback), check the active ongoing notifications with packageName called 'android'. And check this notification's title is your app name or text value is 'App is running in the background' and snooze it.
public class NLService extends NotificationListenerService {
#Override
public void onNotificationRemoved(StatusBarNotification sbn) {}
#Override
public void onListenerConnected() {
super.onListenerConnected();
checkOngoingNotification();
}
#Override
public void onNotificationPosted(StatusBarNotification sbn){
if(sbn.isOngoing()) {
checkOngoingNotification();
return;
}
}
private void checkOngoingNotification() {
StatusBarNotification[] activeNotifications = getActiveNotifications();
Log.i("NLService", "Active notifications size : " + activeNotifications.length);
for (StatusBarNotification statusBarNotification : activeNotifications) {
Log.i("NLService", "notification package : " + statusBarNotification.getPackageName());
Log.i("NLService", "notification id : " + statusBarNotification.getId());
Log.i("NLService", "notification key : " + statusBarNotification.getKey());
Log.i("NLService", "isOngoing : " + statusBarNotification.isOngoing());
Log.i("NLService", "isClearable : " + statusBarNotification.isClearable());
Log.i("NLService", "groupKey : " + statusBarNotification.getGroupKey());
Notification notification = statusBarNotification.getNotification();
CharSequence title = notification.extras.getCharSequence(Notification.EXTRA_TITLE);
CharSequence text = notification.extras.getCharSequence(Notification.EXTRA_TEXT);
if (title != null && text != null) {
Log.i("NLService", "title : " + title);
Log.i("NLService", "text : " + text);
if (statusBarNotification.getPackageName().equals("android") &&
(title.toString().contains("Your App Name") || text.toString().contains("App is running"))) {
long snoozLong = 60000L * 60L * 24L * 20L;
this.snoozeNotification(statusBarNotification.getKey(), snoozLong);
Log.i("NLService", "Snoozed notification : " + title);
}
}
}
}
It turns out startForeground() with channel's IMPORTANCE_MIN is not only one source of the notification. If you call startForeground() and give it notification without setSmallIcon() result will be the same.
https://android.googlesource.com/platform/frameworks/base/+/master/services/core/java/com/android/server/am/ServiceRecord.java#816
Also you could find in logs something like that:
ActivityManager: Attempted to start a foreground service (ComponentInfo{com.example.app/com.example.app.ExampleService}) with a broken notification (no icon: Notification(channel=channel_example pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE))
When I use WhatsApp or Telegram and receive N messages, the notification on Android will show "N new messages" (and it can be expanded).
Telegram shows notification with contentText "7 new messages" on Android. I have achieved this successfully.
However, on my Pebble Time, the last notification is the string of the last message, not "7 new messages".
On Pebble, it shows the 7th (last) message (the censored part is phone number). This is what I want.
I am trying to develop similar feature but have not succeeded. The notification on my Android displays correctly ("N new messages") but on Pebble Time, it's identical (also "N new messages", I want it to be the Nth message).
My app on Pebble. This is NOT what I want.
I have tried to call .notify twice (one contains "N new messages" and the other contains last message) and immediately .cancel the latter but Pebble Time only shows the first one.
If I don't call .cancel on the second notification, my Pebble will show what I want BUT there will be 2 notifications on both Android and Pebble (which I don't want).
How do I achieve similar feature like WhatsApp and Telegram?
Update:
This is the snippet I use (I have used different notification ID)
NotificationCompat.Builder nb = new NotificationCompat.Builder();
...
nb.setContentText("2 new messages");
notificationManager.notify(1, nb.build());
nb.setContentText("B");
notificationManager.notify(2, nb.build());
notificationManager.cancel(2);
Because you use same id for two notification item. you can try with different id for each show notification item.
From Android docs: https://developer.android.com/reference/android/app/NotificationManager.html#notify(int, android.app.Notification)
id: An identifier for this notification unique within your
application.
I'm trying to develop with Android Wear. I tried all the tutorial provided in the documentation, but now I want to try to do something smarter. I'm trying to get back the text that user says (with emulator written by computer keyboard), so I made it with this code:
protected void voiceNotification() {
// Crete intent for the response action
Intent replyIntent = new Intent(this, ReplyActivity.class);
// Adding intent to pending intent
PendingIntent replyPendingIntent = PendingIntent.getActivity(this, 0,
replyIntent, 0);
// Build the notification
NotificationCompat.Builder replyNotificationBuilder = new NotificationCompat.Builder(
this);
replyNotificationBuilder
.setSmallIcon(android.R.drawable.ic_btn_speak_now);
replyNotificationBuilder.setContentTitle("Messaggio");
replyNotificationBuilder.setContentText("Testo del messaggio");
replyNotificationBuilder.setContentIntent(replyPendingIntent);
replyNotificationBuilder.setNumber(++numMessages);
replyNotificationBuilder.setAutoCancel(true);
replyNotificationBuilder.setSound(RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
replyNotificationBuilder.setVibrate(new long[] { 1000, 1000 });
replyNotificationBuilder.setTicker("Hai una nuova notifica!");
// Create remote input
RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
.setLabel(getResources().getString(R.string.reply_label))
.build();
// Create the wearable notification
Notification replyNotification = new WearableNotifications.Builder(replyNotificationBuilder)
.addRemoteInputForContentIntent(remoteInput)
.build();
// Get the instance of NotificationManagerCompat and send my notification
NotificationManagerCompat.from(this).notify(0, replyNotification);
}
With this code on the emulator I'm getting 2 views: one with the text of my notification and a second one in which I can answer to notification with voice (keyboard with emulator). It's working all good, but I want to know if it's possible to get the text I said (wrote with emulator) to do something in my application (I saw on the emulator display that after I said/wrote somethings it appears 2 button "Edit" and "Send", so I think that with button "Send" I can get the text in my application to do something). I try to find out something in the documentation, but I don't find nothing. I hope you can help me to get this text.
You'll need to implement a Broadcast-receiver that listens to the pendingIntent you defined - the reply from the user will be passed in an extra string you defined in the RemoteInput - in your case this would be EXTRA_VOICE_REPLY.
You might want to have a look at these two files someone posted on GitHub in order to understand what is going on.
http://git.io/emKcrw
http://git.io/_PRW_w
I have a problem with appcelerator, regarding to android background service.
The service starts, and when I press the home button, the service is still running, and also when I click the back button.
But when I remove my app from the recent apps list(pressing long home button) , the service stops.
This is my code for the call service:
var SECONDS = 6;
// every 10 minutes
var intent = Titanium.Android.createServiceIntent({
url : 'myservice.js'
});
intent.putExtra('interval', SECONDS * 1000);
intent.putExtra('message_to_echo', num);
//in millimeter
var service = Titanium.Android.createService(intent);
service.addEventListener('resume', function(e) {
// num++;
// alert(num);
});
service.start();
And this is the file service code:
var service = Titanium.Android.currentService;
var intent = service.intent;
var message = intent.getStringExtra("message_to_echo");
var intent = Ti.Android.createIntent({
flags : Ti.Android.FLAG_ACTIVITY_CLEAR_TOP | Ti.Android.FLAG_ACTIVITY_SINGLE_TOP,
// Substitute the correct classname for your application
className : 'com.mappa.angeli.MappaActivity',
});
intent.addCategory(Ti.Android.CATEGORY_LAUNCHER);
// Create a PendingIntent to tie together the Activity and Intent
var pending = Titanium.Android.createPendingIntent({
intent: intent,
flags: Titanium.Android.FLAG_UPDATE_CURRENT
});
// Create the notification
var notification = Titanium.Android.createNotification({
// icon is passed as an Android resource ID -- see Ti.App.Android.R.
// icon: Ti.App.Android.R.drawable.my_icon,
contentTitle: 'Something Happened',
contentText : 'Click to return to the application.',
// tickerText: 'Our app made a notification!',
defaults:Titanium.Android.NotificationManager.DEFAULT_SOUND,
contentIntent: pending
});
// Send the notification.
Titanium.Android.NotificationManager.notify(0, notification);
How can I run the service continously, such as whats app or something like that?
Please help me, it's very important.
Thanks in advance!!!
First let me give you a little perspective about your questions so that you'll be able to understand the logic behind.
My Understanding about your Question and Conclusion
What you are asking for is not possible even in the native app, specifically if you are using the background services.
Why it is doing so?
Because the application is no longer in service anymore. The app is
forcefully removed from the background and hence the OS stops all its
services running in the background to clear the Memory.
What most of the default players do to play music is the use the foreground service and show a notification that the app is running and is processing something in the background (in this case playing the songs).
Options that you could look for
I would suggest you to add up 'push notification service' to your app rather than going in for 'background service'. You will get the notification even if your app is not in the background (same as it is in WhatsApp).
You could follow the below link to implement the GCM push services for android:
http://www.tidev.io/2013/12/20/using-gcm-for-android-push-notifications-with-acs/
Hope this helps.
Good Luck, Cheers