Im building a ping feature for finding a lost phone through bluetooth. I need the phone to sound even though it is set to mute/silent like how the alarm usually works. I thought I could put the streamtype of my notification to AudioManager.STREAM_ALARM but its not working. It only sounds when the phones sound is on. This is how I set it:
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setSmallIcon(R.drawable.ic_spenwallet)
.setContentTitle("Ping")
.setContentText("Device is trying to find your phone.")
.setAutoCancel(false)
.setSound(sound, STREAM_ALARM)
.setVibrate(vibratePattern)
.addAction(cancelAction);
If I try :
Notification notification = builder.build();
notification.audioStreamType = AudioManager.STREAM_ALARM;
Im getting a warning from Android Studio that audioStreamType is deprecated. Is this the case? Any other way to make the notificaiton sound even though silent mode is on? (preferable also vibrate)
I got it working by creating a dedicated mediaplayer for the purpose but I don't think this should be needed. Heres how I did it anyway:
MediaPlayer mediaPlayer = new MediaPlayer();
final String packageName = getApplicationContext().getPackageName();
Uri sound = Uri.parse("android.resource://" + packageName + "/" + R.raw.ping_sound);
try {
mediaPlayer.setDataSource(this, sound);
} catch (IOException e) {
e.printStackTrace();
}
final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mediaPlayer.setLooping(false);
try {
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
}
Using builder.setSound(alarmSound, AudioManager.STREAM_AUDIO) was exactly what I needed to keep my alarm going! Perhaps your issue is with the R.raw.ping_sound sound sample that you are using. After trying a bunch of terrible implementations I found online for alarm notifications (which is where I found Settings.System.DEFAULT_RINGTONE_URI) I followed the official notification documentation and then used the NotificationCompat.Builder documentation for customization.
Here is my working alarm notification:
private void showNotification(){
// Setup Intent for when Notification clicked
Intent intent = new Intent(mContext, MedsActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); // See https://developer.android.com/training/notify-user/navigation for better navigation
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
// Setup Ringtone & Vibrate
Uri alarmSound = Settings.System.DEFAULT_RINGTONE_URI;
long[] vibratePattern = { 0, 100, 200, 300 };
// Setup Notification
String channelID = mContext.getResources().getString(R.string.channel_id_alarms);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext, channelID)
.setContentText(notificationMessage)
.setContentTitle(notificationTitle)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentIntent(pendingIntent)
.setSound(alarmSound, AudioManager.STREAM_ALARM)
.setOnlyAlertOnce(true)
.setVibrate(vibratePattern)
.setAutoCancel(true);
// Send Notification
NotificationManager manager = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, mBuilder.build());
}
Related
The following method shows notification with custom sound and on channel. The issue is that the vibration isn't working. I've tried researching and trying but wasn't successful.
private void showNotification(){
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);
Uri soundUri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.messenger);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, ChannelId)
.setSmallIcon(R.drawable.ic_stat_facebookmessengernotificationicon)
.setContentTitle("John Doe")
.setContentText("Hey, What's Up!")
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
if(soundUri != null){
notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setLegacyStreamType(AudioManager.STREAM_NOTIFICATION)
.build();
NotificationChannel notificationChannel = new NotificationChannel(ChannelId,"Messenger",NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setSound(soundUri,audioAttributes);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{ 100 });
mNotificationManager.createNotificationChannel(notificationChannel);
}
}
mNotificationManager.notify(0, notificationBuilder.build());
}
It's not about custom sound. You are not setting any pattern really. The first value in vibration pattern defines the number of milliseconds to wait before turning the vibrator on. The next value indicates the number of milliseconds for which to keep the vibrator on before turning it off and subsequent values alternate between these two. So in fact pattern sequence means OFF, ON, OFF, ON..., therefore to have any vibration, you need at least two values. I assume you most likely meant this:
notificationChannel.setVibrationPattern(new long[]{ 0, 100 });
Also, calling notificationChannel.enableVibration(true); is redundant as setting valid pattern would automatically enable it (see source).
Hi I want to send image on notification and use data notification
this is my data and
notification comes with picture. but the notification is not clicked
How can i send clikable ?
""data"" : {""click_action"":"".MainActivity"",
""body"" : ""new Symulti update 22!"",
""title"" : ""new"",
""url"":""https://www.blablaaas.com"",
""img_url"":""https://www.image.com/image1""
""}}
I hope you are implementing code for android, so to make notification clickable you just need to add the pendingIntent with destination Activity name so that you will get the clickable action. Use the below code for reference.
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
Intent mainIntent = new Intent(this, TabHostScreen.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent mainPIntent = PendingIntent.getActivity(this, 0, mainIntent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setSmallIcon(getNotificationIcon(builder));
builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
builder.setAutoCancel(true);
//start intent on notification tap (MainActivity)
builder.setContentIntent(mainPIntent);
//custom style
builder.setStyle(new NotificationCompat.DecoratedCustomViewStyle());
builder.setCustomContentView(remoteCollapsedViews);
//builder.setCustomBigContentView(remoteExpandedViews);
long[] pattern = {500, 500, 500};
builder.setVibrate(pattern);
Random rand = new Random();
NOTIFICATION_ID = rand.nextInt(1000);
//notification manager
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
notificationManagerCompat.notify(NOTIFICATION_ID, builder.build());
I want to set default device alarm tone for my scheduled notification as normal notification sounds are hard to notice.
I try to get alarm tone by: Uri alarmTone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
and then simply set it with: builder.setSound(alarmTone);
All I get is vibration with out any alarm tone sound. Any ideas?
Whole code:
private Notification getNotification(String content) {
Uri alarmTone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this,
Receiver.NOTIFICATION_CHANNEL_ID
);
builder.setContentTitle("Title");
builder.setSmallIcon(R.drawable.x);
builder.setPriority(NotificationCompat.PRIORITY_MAX);
builder.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);
builder.setSound(alarmTone);
builder.setContentText(content);
builder.setAutoCancel(true);
return builder.build();
}
Try Below Code:
1.builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
2.mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
Put your preferred alarm in "Res\raw\{your_ringtone}.mp3"
and then do like this:
Notification mNotification = builder.build();
mNotification.sound = Uri.parse("android.resource://"
+ context.getPackageName() + "/" + R.raw.{your_ringtone});
I would like to send a notification to my users to remember them to update the app, but I can't change the app code as obviously the problem is that the app is outdated.
Is there a way to open Play Store via a FCM notification, using clickAction or something like that, without needing to make changes to the app?
you can open play store-specific app using FCM you just need to put following logic in your onMessageReceived of your FCM custom class.
Bellow is the Logic:
public static void openStore(Context context,RemoteMessage remoteMessage) {
Intent intent;
if (remoteMessage.getData().containsKey("appPackageName")) {
final String appPackageName = remoteMessage.getData().get("appPackageName"); // getPackageName()
try {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName));
} catch (android.content.ActivityNotFoundException anfe) {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName));
}
int notificaionId = 1;
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.BigTextStyle bigTextNotiStyle = null;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int color = ContextCompat.getColor(this, R.color.profileFontColor);
NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.ic_notification)
.setContentTitle("" + remoteMessage.getData().get("title"))
.setContentText("" + remoteMessage.getData().get("desc"))
.setStyle(bigTextNotiStyle)
.setAutoCancel(true)
.setColor(color)
.setContentIntent(pIntent)
.setLights(Color.RED, 3000, 3000);
notificationManager.notify(notificaionId, mBuilder.build());
}
}
I'm developing an android app. It receive with a push notification an URL. I need to open the url with chrome. I'm trying with this but is not working
Intent i;
try {
i = new Intent(Intent.ACTION_VIEW);
i.setType("text/html");
i.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName());
i.setComponent(ComponentName.unflattenFromString("com.android.chrome/com.android.chrome.Main"));
i.addCategory("android.intent.category.LAUNCHER");
i.setData(Uri.parse(audioLink));
} catch (ActivityNotFoundException ex) {
i = new Intent(Intent.ACTION_VIEW, Uri.parse(audioLink));
}
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder;
NotificationManager manager;
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
builder = new NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.newmessagetitle))
.setContentText(getString(R.string.newmessage))
.setSmallIcon(R.mipmap.ic_stat_lollipop_icon);
builder.setContentIntent(pendingIntent);
int defaults = 0;
defaults = defaults | Notification.DEFAULT_LIGHTS;
defaults = defaults | Notification.DEFAULT_VIBRATE;
defaults = defaults | Notification.DEFAULT_SOUND;
builder.setDefaults(defaults);
builder.setAutoCancel(true);
manager.notify(notifyID, builder.build());
I really think something like this should work for the intent code. If not, there is probably something wrong with another part of your code:
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(audioLink));
i.setPackage("com.android.chrome");
By the way the ActivityNotFoundException is totally useless in that place, because only Activity.startActivity() throws that exception. If you want to check if Chrome is installed, I would use this: How can I learn whether a particular package exists on my Android device?