in oreo how to enable notification led blink without notification sound,if i disable notification sound led does not blink
Notification notification = new NotificationCompat.Builder(this)
.setLights(0xff00ff00, 3000, 100)
// .setContentTitle("title")
.setSmallIcon(R.drawable.cubs)
.setOngoing(false)
.setSound(null)
.setPriority(Notification.PRIORITY_MAX).build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(notification_id, notification);
From Oreo, the mechanism of NotificationChannel was introduced. You must set the channel and enable lights like this,
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Notification";
String description = "Notification";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel mChannel = new NotificationChannel("someChannelID", name, importance);
mChannel.setDescription(description);
mChannel.setShowBadge(true);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.setSound(uri);
if (notificationManager != null) notificationManager.createNotificationChannel(mChannel);
Notification notification = new Notification.Builder(context, "someChannelID")
.setContentTitle(contentTitleText)
.setContentText(contentContentText)
.setNumber(1)
.setSmallIcon(whiteLogo)
.setBadgeIconType(whiteLogo)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.build();
if (notificationManager != null) {
notificationManager.notify(notification_id, notification);
}
}
else {
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setSmallIcon(whiteLogo)
.setLargeIcon(largeIcon)
.setContentTitle(contentTitleText)
.setContentText(contentContentText)
.setOngoing(false)
.setNumber(1)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true)
.setLights(colorPPDOrange, 1000, 2000);
.setSound(uri);
.setContentIntent(pendingIntent)
.build();
if (notificationManager != null) {
notificationManager.notify(notification_id, notification);
}
}
This is my code
`import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public abstract class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String channelId = "test-channel";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel newIncidentChannel = new NotificationChannel(channelId,
"Test Channel",
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(newIncidentChannel);
}
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Test")
.setContentText("Text")
.setSound(RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true);
int NOTIFICATION_ID = (int) (System.currentTimeMillis()%10000);
notificationManager.notify("test", NOTIFICATION_ID, notificationBuilder.build());
}
}
`
Related
I tried to make notifications but there is no notifications when I click the button, nor errors.
I enabled the notification from phone setting.
my code
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class notification_page extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification_page);
// Button notify = findViewById(R.id.notify);
}
NotificationManager manager;
int id =0;
public void notify(View view) {
NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this);
nBuilder.setContentTitle("Hello").setContentText("Notificaction!!").setSmallIcon(R.drawable.common_google_signin_btn_icon_dark);
manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(id,nBuilder.build());
id++;
}
public void cancel(View view) {
manager.cancelAll();
}
}
XML button android:onClick="notify"
and there is no errors in the Logcat
First of all you have to create the notification channel:
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("1000", "channel_name", importance);
channel.setDescription("channel_description");
channel.setSound(null, null);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
Change the NotificationManager to be:
NotificationManagerCompat manager;
then modify your notify() function like this:
public void notify(View view) {
createNotificationChannel(); //don't forget to create the channel
NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this, "1000"); //channelId should be same as the one created above
nBuilder.setContentTitle("Hello").setContentText("Notificaction!!").setSmallIcon(R.drawable.common_google_signin_btn_icon_dark);
manager = NotificationManagerCompat.from(getApplicationContext());
manager.notify(id, nBuilder.build());
id++;
}
I am trying to set notification but something gone wrong when I set vibration for my notification.
Even though it still shows notification there is no vibration.
I have already added user-permission, android.permission.VIBRATE in manifest
here is the code,
class MainActivity : AppCompatActivity() {
lateinit var notificationManager: NotificationManager
lateinit var notificationChannel: NotificationChannel
lateinit var builder: Notification.Builder
val channelId = "com.example.notificationtest"
val descr = "My notification"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
init()
}
fun init() {
val show = findViewById<Button>(R.id.show)
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
show.setOnClickListener {
val intent = Intent(applicationContext, MainActivity::class.java)
val pendingIntent =
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationChannel =
NotificationChannel(channelId, descr, NotificationManager.IMPORTANCE_HIGH)
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.RED
notificationChannel.enableVibration(true)
notificationManager.createNotificationChannel(notificationChannel)
builder = Notification.Builder(this, channelId)
.setContentTitle("Android")
.setContentText("new message")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentIntent(pendingIntent)
.setCategory(NotificationCompat.CATEGORY_ALARM)
}
notificationManager.notify(0, builder.build())
}
}}
Please use the following code snippet for Android Notification with Vibration setup.
NotificationManager notificationManager;
NotificationCompat.Builder mBuilder = null;
String channelId = "com.example.notificationtest";
String descr = "My notification";
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel = new NotificationChannel(channelId, descr, importance);
notificationChannel.enableLights(true);
notificationChannel.enableVibration(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
assert notificationManager != null;
notificationManager.createNotificationChannel(notificationChannel);
mBuilder = buildNotification(this, "new Message", "Android", null, R.drawable.home, channelId);
mBuilder.setChannelId(channelId);
} else {
mBuilder = new NotificationCompat.Builder(this, channelId);
mBuilder.setSmallIcon(R.drawable.home);
mBuilder.setContentTitle("Android")
.setContentText("new Message")
.setAutoCancel(false)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
}
notificationManager.notify(0, mBuilder.build());
And create a new method like,
#TargetApi(Build.VERSION_CODES.O)
public NotificationCompat.Builder buildNotification(Context mContext, String text, String fromnumber, PendingIntent pendingIntent, int icon, String channelId) {
return new NotificationCompat.Builder(mContext, channelId)
.setSmallIcon(icon)
.setContentTitle(fromnumber)
.setSubText(mContext.getString(R.string.app_name))
.setContentText(text)
.setAutoCancel(true)
.setOngoing(true)
.setAutoCancel(true);
}
My program did not show the notificaion bar in android compilesdk28. My code is below. How can I solve this and how do I display the notification in All mobiles?
final String url = userDetailsItem.getIp();
System.out.println("url..."+url);
if (Remaindays .equals("53"))
System.out.println("url..."+url);
{
Context context = null;
String CHANNEL_ID = createNotificationChannel(context);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,CHANNEL_ID)
.setSmallIcon(R.drawable.lesr)
.setContentTitle("my app")
.setContentText(""+url)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(Remaindays+"Days "))
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
}
public static String createNotificationChannel(Context context) {
// NotificationChannels are required for Notifications on O (API 26) and above.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// The id of the channel.
String CHANNEL_ID = "Channel_id";
// The user-visible name of the channel.
CharSequence channelName = "Application_name";
// The user-visible description of the channel.
String channelDescription = "Application_name Alert";
int channelImportance = NotificationManager.IMPORTANCE_DEFAULT;
boolean channelEnableVibrate = true;
// int channelLockscreenVisibility = Notification.;
// Initializes NotificationChannel.
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, channelName, channelImportance);
notificationChannel.setDescription(channelDescription);
notificationChannel.enableVibration(channelEnableVibrate);
// notificationChannel.setLockscreenVisibility(channelLockscreenVisibility);
// Adds NotificationChannel to system. Attempting to create an existing notification
// channel with its original values performs no operation, so it's safe to perform the
// below sequence.
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
assert notificationManager != null;
notificationManager.createNotificationChannel(notificationChannel);
return CHANNEL_ID;
} else {
// Returns null for pre-O (26) devices.
return null;
}
}
API => 28 requires Notification Channel
https://developer.android.com/reference/android/app/NotificationChannel
if you want video links
https://www.youtube.com/watch?v=tTbd1Mfi-Sk
Template:
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent intent = new Intent(getApplicationContext(), DummychatActivty.class);
intent.putExtra("user_id", Config.to_user_id);
intent.putExtra("profile_img_Url", Config.profile_img_Url);
intent.putExtra("user_name", Config.to_user_name);
intent.putExtra("user_image_url", Config.to_user_image_url);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String CHAT_NOTIFICATION_CHANNEL_ID = "101";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
#SuppressLint("WrongConstant") NotificationChannel Chat_notificationChannel = new NotificationChannel(CHAT_NOTIFICATION_CHANNEL_ID, "Chat Notifications", NotificationManager.IMPORTANCE_MAX);
//Configure Notification Channel
// notificationChannel.setDescription("General Notifications");
Chat_notificationChannel.setLightColor(Color.RED);
Chat_notificationChannel.enableLights(true);
Chat_notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
Chat_notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(Chat_notificationChannel);
}
NotificationCompat.Builder chat_notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), CHAT_NOTIFICATION_CHANNEL_ID)
.setVibrate(new long[]{0, 100})
.setPriority(Notification.PRIORITY_MAX)
.setLights(Color.RED, 3000, 3000)
.setAutoCancel(true)
.setContentTitle(Config.fcm_headline)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setColor(getResources().getColor(R.color.colorPrimary))
.setSmallIcon(R.drawable.push)
.setLargeIcon(bitmap)
.setGroup("CHAT")
.setContentText(Config.content)
.setAutoCancel(true)
.setSound(defaultSound);
chat_push_count = chat_push_count + 1;
notificationManager.notify(chat_push_count, chat_notificationBuilder.build());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
chat_notificationBuilder.setChannelId("101");
}
private void sendNotification(Notification nobj) {
Intent intent = new Intent(context, NotificationActivity.class);
intent.putExtra("Notification", "click");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri =
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(context, defaultSoundUri);
r.play();
NotificationCompat.Builder notificationBuilder = new
NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(Html.fromHtml(nobj.title))
.setContentText(Html.fromHtml(nobj.desc))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setChannelId("my_channel_01")
.setContentIntent(pendingIntent);
generateNotification(notificationBuilder);
}
Please check this you can get help , its working code in my live project
public void generateNotification(NotificationCompat.Builder notificationBuilder) {
NotificationManager notificationManager =
(NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
String CHANNEL_ID = "my_channel_01";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = context.getString(R.string.app_name);
// The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name,
importance);
// Create a notification and set the notification channel.
notificationManager.createNotificationChannel(mChannel);
}
PreferenceHelper preferenceHelper = PreferenceHelper.getInstance(context);
preferenceHelper.setNotificationId(preferenceHelper.getNotificationId() + 1);
notificationManager.notify(preferenceHelper.getNotificationId(),
notificationBuilder.build());
}
I want to create a push notification where Admin can send notification to all users. I found a tutorial and follow it but it doesn't work. I'm not sure where I did wrong but I got error that said
Developer Warning for package "... " Failed to post notification on channel "null"
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String tittle = ed1.getText().toString().trim();
String subject = ed2.getText().toString().trim();
String body = ed3.getText().toString().trim();
NotificationManager notif = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify = new Notification.Builder
(getApplicationContext()).setContentTitle(tittle).setContentText(body).
setContentTitle(subject).setSmallIcon(R.drawable.ps).build();
notify.flags |= Notification.FLAG_AUTO_CANCEL;
notif.notify(0, notify);
}
});
After Oreo SDK you have to create Notification channel in order to show a notification, check this method for reference:
/**
*
* #param context
* #param title --> title to show
* #param message --> details to show
* #param intent --> What should happen on clicking the notification
* #param reqCode --> unique code for the notification
*/
public void showNotification(Context context, String title, String message, Intent intent, int reqCode) {
SharedPreferenceManager sharedPreferenceManager = SharedPreferenceManager.getInstance(context);
PendingIntent pendingIntent = PendingIntent.getActivity(context, reqCode, intent, PendingIntent.FLAG_ONE_SHOT);
String CHANNEL_ID = "channel_name";// The id of the channel.
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.mipmap.notification_logo)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Channel Name";// The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
notificationManager.createNotificationChannel(mChannel);
}
notificationManager.notify(reqCode, notificationBuilder.build()); // 0 is the request code, it should be unique id
Log.d("showNotification", "showNotification: " + reqCode);
}
How to use this method:
int reqCode = 1;
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
showNotification(this, "Title", "This is the message to display", intent, reqCode);
public void sendNotification (String message, String title ){
Intent intent = new Intent(getApplicationContext(), CampaignActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent,
PendingIntent.FLAG_IMMUTABLE);
String channelId = "some_channel_id";
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_launcher_round)
// .setContentTitle(getString(R.string.app_name)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
assert notificationManager != null;
notificationManager.createNotificationChannel(channel);
}
assert notificationManager != null;
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
I have a code for Notifications in JobService:
public class MyJobService extends JobService {
String NOTIFICATION_CHANNEL_ID = "Nilesh_channel";
#Override
public boolean onStartJob(JobParameters params) {
Intent medication = new Intent(this, Medication.class);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Your Notifications",
NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription("");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
mNotificationManager.createNotificationChannel(notificationChannel);
}
// to diaplay notification in DND Mode
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = mNotificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID);
channel.canBypassDnd();
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setContentTitle(Medication.medication.get(i).getDrug())
.setContentText("Время приема " + Medication.medication.get(i).getTime())
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.logonotification)
.setAutoCancel(true);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(medication);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
notificationBuilder.setContentIntent(resultPendingIntent);
startForeground(1000, notificationBuilder.build());
mNotificationManager.notify(1000, notificationBuilder.build());
Toast.makeText(this, "Запуск сервиса", Toast.LENGTH_SHORT).show();
}
But line startForeground(1000, notificationBuilder.build()) not working, I have not see my notification in notification bar. If I deleting this line, notification is shown, but it is not foreground.
In Manifest - <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
How can I fix it?