Im trying to show notification on wear watch anything is goes correct but My Remote input choice are not visible on samsung watch it will show only title and Body my input choice are not visible on it.
It appears on my phone, but it doesn't appear on my watch connected to my phone. Is there something missing from the code that's required for Android Wear?
I'm using this code for display notification on watch
public void getNotifcation(){
RemoteInput ri = new RemoteInput.Builder(KEY_TEXT_REPLY) .setLabel(body) .setChoices(replyChoices) .build();
NotificationCompat.Action a =
new NotificationCompat.Action.Builder(R.drawable.ic_send_arraow_red,
null, getReplyPendingIntent())
.addRemoteInput(ri)
.build();
Uri defaultSoundUri = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_NOTIFICATION);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
.setSmallIcon(R.drawable.ic_noti_icon)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_notification))
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setVibrate(new long[]{200, 400, 600, 800})
.setOngoing(false)
.setGroup("GROUP")
.setGroupSummary(false)
.setContentIntent(notifyPendingIntent)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.addAction(a)
.extend(new NotificationCompat.WearableExtender().addAction(a));
NotificationCompat.WearableExtender extender =
new NotificationCompat.WearableExtender();
extender.addAction(new NotificationCompat.Action.Builder(R.drawable.ic_send_arraow_red, "send" ,
getReplyPendingIntent())
.addRemoteInput(ri).build());
notificationBuilder.extend(extender);
//Wear OS requires a hint to display the reply action inline.
NotificationCompat.Action.WearableExtender actionExtender =
new NotificationCompat.Action.WearableExtender()
.setHintLaunchesActivity(true)
.setHintDisplayActionInline(true);
notificationBuilder.addAction(noti.extend(actionExtender).build());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(channelId, "Notifications",
NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription(messageBody);
notificationChannel.enableLights(true);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{200, 400, 600, 800});
notificationChannel.setLightColor(Color.RED);
if (defaultSoundUri != null) {
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
notificationChannel.setSound(defaultSoundUri, audioAttributes);
}
notificationManager.createNotificationChannel(notificationChannel);
}
notificationManager.notify(notificationId notificationBuilder.build());
}
and here is my pending intent to get choice on wear
public void getReplyPendingIntent(){
Intent intent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // start a // (i) broadcast receiver which runs on the UI thread or // (ii) service for a background task to b executed,will be doing a broadcast receiver
intent = new Intent(context, FcmBroadcastReceiver.class);
intent.setAction(REPLY_ACTION);
intent.putExtra(KEY_NOTIFICATION_ID, notificationId);
intent.putExtra(KEY_TEXT_REPLY, KeyReply);
return PendingIntent.getBroadcast(getApplicationContext(), 100, intent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
} else {
// start your activity
intent = Act_Home.getReplyMessageIntent(this, notificationId, 123);
return PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
}
}
Related
Be informed, we are trying to send notification with a button using Addaction. The button Start has a intent that directly opens a url in a webview, which works perfectly fine with code given below
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("uri", uri);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//int notification_id = nid!=null ? Integer.parseInt(nid) : MainActivity.ASWV_FCM_ID;
int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
int notification_id=m;
Intent buttonIntent = new Intent(this, ButtonReceiver.class);
buttonIntent.putExtra("notificationId",notification_id);
//Create the PendingIntent
PendingIntent btPendingIntent = PendingIntent.getBroadcast(this, 0, buttonIntent,0);
String channelId = MainActivity.asw_fcm_channel;
Uri soundUri=Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/pristine");
NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.mipmap.ic_launcher, tag, pendingIntent).build();
NotificationCompat.Action action1 = new NotificationCompat.Action.Builder(R.mipmap.ic_launcher, "Dismiss", btPendingIntent).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder1 = new NotificationCompat.Builder(this, MainActivity.asw_fcm_channel)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(message)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setAutoCancel(false)
.addAction(action)
.addAction(action1)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(channelId, "My Notifications", NotificationManager.IMPORTANCE_HIGH);
AudioAttributes attributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
// Configure the notification channel.
mChannel.setDescription("Common notifications");
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.setSound(soundUri, attributes);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
mChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
notificationManager.createNotificationChannel(mChannel);
}
Notification noti = notificationBuilder1.build();
noti.flags = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(notification_id, notificationBuilder1.build());
Update:
I'm sorry but i'm not actually a expert or a regular coder in android java, so you gotta help me here a little bit more. I have passed on my notification id down in the code, but the problem here is how do i invoke removeNotification in the pendingIntent which also carries my url to where the link will go onclick
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("uri", uri);
intent.putExtra("notificationId",notification_id);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
public static void removeNotification(int notification_id) {
try {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(notification_id);
}
} catch (Exception e) {
}
}
But we also like the notification to be dismissed simultaneously onclick. We tried using .setAutocancel (true) and notification flags like FlAG_AUTO_CANCEL, but the button just does first action right of going into webview url and not cancelling the notification.
Shall appreciate should you help us out.
Send the notification id in the intent to activity and use the code to cancel.
public static void removeNotification(Context context, int notification_id) {
try {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(notification_id);
}
} catch (Exception e) {
Timber.d(e);
}
}
Intent intent = getIntent();
if (intent.hasExtra("notificationID")) {
removeNotification(context, intent.getIntExtra("notificationID", 0));
}
Be informed we have attached a pendingIntent that opens a url inside the webview of our app on addAction click. The intent works perfectly fine when app is closed, but when app is in foreground or background,the intent just opens the app without going to the said url.
The code is as given below
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("uri", uri);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder1 = new NotificationCompat.Builder(this, MainActivity.asw_fcm_channel)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(message)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setAutoCancel(false)
.addAction(action)
.addAction(action1)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(channelId, "My Notifications", NotificationManager.IMPORTANCE_HIGH);
AudioAttributes attributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
// Configure the notification channel.
mChannel.setDescription("Common notifications");
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.setSound(soundUri, attributes);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
mChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
notificationManager.createNotificationChannel(mChannel);
}
Notification noti = notificationBuilder1.build();
noti.flags = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(notification_id, notificationBuilder1.build());
Intent intent = new Intent(this, SplashActivity.class);
Bundle bundle = new Bundle();
bundle.putString("splash", psd);
bundle.putString("targetId", targetId);
intent.putExtras(bundle);
intent.setAction(psd);
intent.setAction(targetId);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), id, intent,
0);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(android.R.drawable.ic_notification_overlay)
.setContentTitle(title)
.setContentText(remoteMessage.getData().get("body"))
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle().bigText("" + remoteMessage.getData().get("body")))
.setContentIntent(pendingIntent)
.setSound(defaultSoundUri);
NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
String[] events = new String[6];
inboxStyle.setBigContentTitle("" + title);
for (int i = 0; i < events.length; i++) {
inboxStyle.addLine(events[i]);
}
//.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(id, notificationBuilder.build());
It used to be working, no clue what's happening on new Android devices like Android O. I didn't try back going to old devices, but it's happening on pixel.
Starting in Android 8.0 (API level 26), all notifications must be
assigned to a channel. For each channel, you can set the visual and
auditory behavior that is applied to all notifications in that
channel. Then, users can change these settings and decide which
notification channels from your app should be intrusive or visible at
all
Notification channels
try this code it works perfectly for me.
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.addLine(message);
NotificationManager mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(),"channel_01")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText(message)
.setContentIntent(resultPendingIntent)
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_HIGH)
.setChannelId("channel_01")
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setStyle(inboxStyle)
.setContentTitle(Title);
mNotificationManager.notify(Notification_ID, mBuilder.build());
NotificationChannel channel = new NotificationChannel(Notification_ID, "Playback Notification", NotificationManager.IMPORTANCE_HIGH);
channel.enableLights(true);
channel.enableVibration(true);
channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
assert mNotificationManager != null;
mBuilder.setChannelId("channel_01");
mNotificationManager.createNotificationChannel(channel);
}else {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(),Notification_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(Title)
.setContentIntent(resultPendingIntent)
.setContentText(message)
.setStyle(inboxStyle)
.setSound(soundUri)
.setAutoCancel(true);
mNotificationManager.notify(Notification_ID, mBuilder.build());
}
I just figured this one out myself, you need to setup Channels in Oreo, see my previous post on the issue - I though it was a master-detail issue!!! Turns out Oreo has an additional attribute you need but it seems to fail silently if you don't provide it.
Set up notification channels. Example code below
public static void createNotificationChannel(final Context context, final
String channelId, final CharSequence channelName, final String channelDescription, final int importance, final boolean showBadge) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
postAsyncSafely("createNotificationChannel", new Runnable() {
#Override
public void run() {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
notificationChannel.setDescription(channelDescription);
notificationChannel.setShowBadge(showBadge);
notificationManager.createNotificationChannel(notificationChannel);
Logger.i("Notification channel " + channelName.toString() +
" has been created");
}
});
}
}catch (Throwable t){
Logger.v("Failure creating Notification Channel",t);
}
}
I used firebase cloud functions to the device to device notifications but notifications not working on versions below oreo.
private void sendNotification1(String notificationTitle, String notificationBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this, 0,new Intent(this, MyReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
RemoteInput remoteInput = new RemoteInput.Builder(NOTIFICATION_REPLY)
.setLabel("Respond to Message")
.build();
NotificationCompat.Action action =
new NotificationCompat.Action.Builder(android.R.drawable.ic_delete,
"Reply Now...", pendingIntent1)
.addRemoteInput(remoteInput)
.build();
Here I am creating notification channel for oreo
NotificationChannel notificationChannel = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,
CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_HIGH );
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setShowBadge(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
}
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ONE_ID)
.setAutoCancel(true) //Automatically delete the notification
.setSmallIcon(R.mipmap.ic_launcher)//Notification icon
.addAction(action)
.addAction(R.drawable.accept, "ACCEPT", pendingIntent)
.addAction(R.drawable.delete, "DECLINE", pendingIntent)
.setContentIntent(pendingIntent)
.setContentTitle(notificationTitle)
.setContentText(notificationBody)
.setSound(defaultSoundUri);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager.createNotificationChannel(notificationChannel);
}
notificationManager.notify(1, notificationBuilder.build());
}
I also mentioned the entry in Manifest too. And receiving notifications on oreo properly but below oreo version notifications are invisible. please help me out.
In Oreo Notification is showing using chanel id as in your code. But below oreo version chanel id is not required to use.
Check if your getting log while you receive notification from friebase. If you are getting log then notification functionality is working properly, you are not correctly notifying the device.
When the application is in background I am not able to get the action buttons.
It works only when the app is in the foreground.
Can anyone help me with this?
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// create channel in new versions of android
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel("Test", "Test Notification", importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notificationManager.createNotificationChannel(notificationChannel);
}
// show notification
Intent positive = new Intent(this, NotificationDismissedReceiver.class);
positive.putExtra("notiID", "1");
positive.setAction("Info");
Intent redoplacement = new Intent(this, NotificationDismissedReceiver.class);
redoplacement.putExtra("notiID", "2");
redoplacement.setAction("Call");
PendingIntent pIntent_positive = PendingIntent.getBroadcast(this, 1, positive, PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent redoplacementintent = PendingIntent.getBroadcast(this, 2, redoplacement, PendingIntent.FLAG_CANCEL_CURRENT);
Intent intent = new Intent(this, SettingsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// 0 is request code
PendingIntent pendingIntent1 = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, "Test")
.setSmallIcon(R.drawable.logo)
.setContentTitle(getString(R.string.app_name))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent1)
.setPriority(Notification.PRIORITY_MAX)
.setWhen(0)
.addAction(0, "Info", pIntent_positive)
.addAction(0, "Call", redoplacementintent);
// 0 is id of notification
notificationManager.notify(0, notificationBuilder.build());
}