Im looking for a way to send 2 different notifications out from an app. On the phone the notification will display one notification but on the watch it needs 2 display 3 separate notifications. Is there a way to send these to the watch so that they don't display on the phone as well?
Thanks
You should be able to send separate notifications to the watch. Google walks through the process of how to add wearble features to notifications here:
http://developer.android.com/training/wearables/notifications/index.html
This page shows you how to send a wearable only notification:
http://developer.android.com/training/wearables/notifications/creating.html
In particular this section from the above site:
Specify Wearable-only Actions
If you want the actions available on the wearable to be different from
those on the handheld, then use WearableExtender.addAction(). Once you
add an action with this method, the wearable does not display any
other actions added with NotificationCompat.Builder.addAction(). That
is, only the actions added with WearableExtender.addAction() appear on
the wearable and they do not appear on the handheld.
// Create an intent for the reply action
Intent actionIntent = new Intent(this, ActionActivity.class);
PendingIntent actionPendingIntent =
PendingIntent.getActivity(this, 0, actionIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Create the action
NotificationCompat.Action action =
new NotificationCompat.Action.Builder(R.drawable.ic_action,
getString(R.string.label), actionPendingIntent)
.build();
// Build the notification and add the action via WearableExtender
Notification notification =
new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.ic_message)
.setContentTitle(getString(R.string.title))
.setContentText(getString(R.string.content))
.extend(new WearableExtender().addAction(action))
.build();
Related
about event for click on notification:
I am searching since yesterday about that , what I found/understood: in FirebaseMessagingService will receive notification data , after will fire local notification, so need to add event in that local notification, I tried to add that many times with many ways but nothing worked …
after I tried to deleted notification files (firebase notification files, and local notification files) but still can receive notification. do you know how to know if the user clicked on the notification ?
To receive messages, use a service that extends FirebaseMessagingService. Your service should override the onMessageReceived and onDeletedMessages callbacks. It should handle any message within 20 seconds of receipt (10 seconds on Android Marshmallow). The time window may be shorter depending on OS delays incurred ahead of calling onMessageReceived. After that time, various OS behaviors such as Android O's background execution limits may interfere with your ability to complete your work.
For further info. you can visit the official website:
Link: https://firebase.google.com/docs/cloud-messaging/android/receive
Hope you'll get your answer here.
Step 1:
// Create an Intent for the activity you want to start
Intent intent = new Intent(this, MainActivity.class);
Step 2:
// Create the PendingIntent
PendingIntent pendingIntent = PendingIntent.getActivity(this, Calendar.getInstance().get(Calendar.MILLISECOND), intent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
Step3:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setContentIntent(pendingIntent);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, builder.build());
Whenever a user clicks on notification MainActivity will be opened.
Here is details implementation of Android Notification Sample https://github.com/android/user-interface-samples/tree/master/Notifications
I have implemented firebasse notification functionality in my chat app and everything is running fine but now while testing in Android os 8.0 and 8.1 when app is in background and if user is getting 4 or more than 4 notification then its combined in group and when user click on group then not getting intent and app is restarted.
If user tap on single notification then I am able to send him in specific screen.
I want Notification data or chat id so I can send him in specific screen but not getting any data in intent.
I have searched similar kind of question in stackoverflow but still not getting proper result.
Android: Clicking Grouped Notifications Restarts App
How to open non-launcher activity on notification group click
You can use setContentIntent to set the value of Intent like below
Intent intent = new Intent(this, SecondActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.your_notification_icon)
.setContentTitle("Notification Title")
.setContentText("Notification ")
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());
It will open SecondActivity (Replace the SecondActivity with your own activity)
I am having the same issue causing app restart in Oreo, after many tries i noticed that if when creating the notification i call .setGroup(string) the system no longer groups the notifications.I am creating the notifications from a service. This is not a solution i know but not grouping is a lesser evil than app restart.
I am creating notification from my app on Phone in the below way.
private void launchMicroAppFromWearableNotif() {
int notificationId = 001;
// The below action has been defined in the micro app
Intent i = new Intent("com.microapp.action.PLAY_MUSIC");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent playPendingIntent =
PendingIntent.getActivity(this, 0, i, 0);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.play_music)
.setContentTitle("Play Music")
.setContentText("Play Music on Wear")
.setContentIntent(playPendingIntent)
.addAction(R.drawable.play_music, getString(R.string.act1), playPendingIntent);
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
// Build the notification and issues it with notification manager.
notificationManager.notify(notificationId, notificationBuilder.build());
}
I have created a wearable micro app which has an activity(PlayActivity) with action "com.microapp.action.PLAY_MUSIC" defined in wearable Manifest.
When I take the action from notification on the wearable I expect the activity form the micro app to be launched. But nothing happens. Could someone please help on this. Is it the right way to do it?
Actions that you add to a notification are for the device that the notification was created on, so if you create a notification on your phone, when you click on it on your watch, the action will be sent back to your phone to be executed.
If you want to open your activity on your watch, you cannot achieve that by sending a notification from your phone in the manner you have done. You have a couple of options: for example you can send a message from your phone to your watch and then have the wear side of your app catch that message and put out a "local" notification on the watch; then the actions on that notification are local to your watch and you can open your desired activity via the actions in that notification. If you directly want to open an activity on your watch from your phone (i.e. you don't need a notification to show up on your watch), then you can handle the message that I talked about on your watch and instead of creating a notification there, simply open the desired activity.You may find the WearCompanionLibrary useful in handling some of these cases.
I just succesfully created a bunch of pages notifications on my Wear device.
The only problem is that the PendingIntent does not seems to start an Activity
(which is of course declared in Manifest).
Here is my code:
List extras = new ArrayList();
Intent viewIntent = new Intent(getApplicationContext(), DetailActivity.class);
viewIntent.putExtra("KEY", "TEST123");
//Note: I also tried: Intent viewIntent = new Intent(getApplicationContext(), DetailActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent viewPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, viewIntent, 0);
for (Route aRoute : myRoutes) {
Notification aNotif = new NotificationCompat.Builder(getApplicationContext())
.setContentTitle("BUS " + aRoute.route_short_name)
.setContentText(aRoute.directions.get(0).trip_headsign)
.setLargeIcon(bitmap)
.setContentIntent(viewPendingIntent)
.setSmallIcon(R.mipmap.ic_launcher).build();
extras.add(aNotif);
}
NotificationCompat.Builder builder1 = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(desc)
.setContentIntent(viewPendingIntent)//Just in case
.setSmallIcon(R.mipmap.ic_launcher);
Notification notification = builder1
.extend(new NotificationCompat.WearableExtender()
.addPages(extras))
.setContentIntent(viewPendingIntent)//Just in case
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(0, notification);
When I press on a Notification, I expect the intent to start, but nothing happens..
Any suggestion is welcome.
EDIT:
This code works, just after the notification, so, the second activity can easily be launched withour bug:
startActivity(viewIntent);
EDIT2:
There is now an "open" button at the end that works fine, but still nothing happens on individual notifications (every pages)
Pages are not clickable - on Android Wear, only actions are clickable. For phone generated notifications, those only appear after all pages
If you have a content intent on your phone generated notification, that will always appear as an 'Open on phone' action. There is no way to disable this unless you remove your content intent (making the notification unclickable on phones).
I say 'phone generated' as you can also create a Wear app. By using the data layer to push messages to your Wear app, the Wear app can then build custom notifications. These notifications allow you to use setDisplayIntent() and display activities inline (either as the main page or as separate pages). These activities can, of course, contain any View you want, including actions to perform any action (such as send a message back to the phone to start a particular activity).
Note that because pages are not clickable by default, styling of a custom notification should make it very obvious that the items are clickable. Rather than using a custom notification activity, you may consider using setContentAction() to display the action icon inline with the rest of the layout - this removes the action as a separate element past the action and places it directly on the notification/page.
I'm building an android-app with wear-support and having the following problem when I add a notification with the voice-action specified here:
http://developer.android.com/training/wearables/notifications/voice-input.html
// Create an intent for the reply action
Intent replyIntent = new Intent(mainActivity, VoiceActivity.class);
replyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent replyPendingIntent =
PendingIntent.getActivity(mainActivity, 0, replyIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Create the reply action and add the remote input
NotificationCompat.Action voice_action =
new NotificationCompat.Action.Builder(R.drawable.ic_action_mic,
getString(R.string.label_voice), replyPendingIntent)
.addRemoteInput(remoteInput)
.build();
Everytime the wearable calls the voice action / pending intent(?) my activity starts in onCreate() and opens my MainActivity-UI on the phone! But the thing is, the user is working simultaneously on the phone and don't want to get interrupted by a simple voice-action.
Any ideas how to pretend the voice-command from opening the app on phone when it's "minimized"?