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.
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.
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();
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 have a method which receives text from a push notification, via the Parse API, and packages it into a notification object. Pretty standard stuff. My problem is that I'm trying to use a BigTextStyle to display my notification in the list, but it refuses to do so, and only shows one line of text and the two-finger gesture does not cause it to expand.
However, if I tap the notification, which opens the app, then return to the notification list, it is displayed in the BigTextStyle and is responsive to gestures. So, my guess is that somehow tapping on the notification is activating it and allowing the BigTextStyle code to kick in.
I like that tapping on the notification opens the app, but I don't want to force my users to open the app then close it again to see the full text of my messages. So is there a way I could either make the notification display in the BigTextStyle format from the start, or to make it so that the first click "activates" the notification, allowing the full message text to be seen, and then a second click opens the app? Any help would be appreciated.
Here is my code from the Notification method:
public void receiveNotification() {
NotificationCompat.BigTextStyle bts = new NotificationCompat.BigTextStyle();
bts.bigText(SplashActivity.globalDataString);
bts.setSummaryText("Tap to open app, swipe to dismiss message");
NotificationCompat.Builder m = new NotificationCompat.Builder(this);
m.setContentTitle("New Push Notification")
.setContentText(SplashActivity.globalDataString)
.setSmallIcon(R.drawable.app_icon)
.setStyle(bts)
.build();
Intent openApp = new Intent(this, MenuActivity.class);
// This ensures that navigating backward from the Activity leads out of
// the application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MenuActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(openApp);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
m.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(pushMessageID, m.build());
pushMessageID++;
//reset notification
flag1 = false;
}
EDIT: I think my problem is with where I'm calling my receiveNotification() method from. Right now I have it in the onCreate() method of my app's starting activity, which doesn't make much sense looking back. Should I put it in my broadcastReceiver class, or would there be a better place to put it?
Yes, the creation and display of the notification is usually done either in the broadcast receiver, or in an intent service started by the broadcast receiver. Only when the user taps the notification, the relevant activity is launched. You can see Google's client code sample here.