public void CreateNotification(String title, String message) { try { var intent = new Intent(global::Android.App.Application.Context, typeof(MainActivity)); intent.AddFlags(ActivityFlags.ClearTop); intent.PutExtra("title", message); var pendingIntent = PendingIntent.GetActivity(global::Android.App.Application.Context, 0, intent, PendingIntentFlags.OneShot); var sound = global::Android.Net.Uri.Parse(ContentResolver.SchemeAndroidResource + "://" + global::Android.App.Application.Context.PackageName + "/" + Resource.Raw.alert); // Creating an Audio Attribute var alarmAttributes = new AudioAttributes.Builder() .SetContentType(AudioContentType.Sonification) .SetUsage(AudioUsageKind.Notification).Build(); mBuilder = new NotificationCompat.Builder(global::Android.App.Application.Context); mBuilder.SetSmallIcon(Resource.Drawable.abc); mBuilder.SetContentTitle(title) .SetSound(sound) .SetAutoCancel(true) .SetContentTitle(title) .SetContentText(message) .SetChannelId(NOTIFICATION_CHANNEL_ID) .SetPriority((int)NotificationPriority.High) .SetVibrate(new long[0]) .SetDefaults((int)NotificationDefaults.Sound | (int)NotificationDefaults.Vibrate) .SetVisibility((int)NotificationVisibility.Public) .SetSmallIcon(Resource.Drawable.abc) .SetContentIntent(pendingIntent); NotificationManager notificationManager = global::Android.App.Application.Context.GetSystemService(Context.NotificationService) as NotificationManager; if (global::Android.OS.Build.VERSION.SdkInt >= global::Android.OS.BuildVersionCodes.O) { NotificationImportance importance = global::Android.App.NotificationImportance.High; NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, title, importance); notificationChannel.EnableLights(true); notificationChannel.EnableVibration(true); notificationChannel.SetSound(sound, alarmAttributes); notificationChannel.SetShowBadge(true); notificationChannel.Importance = NotificationImportance.High; notificationChannel.SetVibrationPattern(new long[] { 100, 200, 300, 400, 500, 400, 300, 200, 400 }); if (notificationManager != null) { mBuilder.SetChannelId(NOTIFICATION_CHANNEL_ID); notificationManager.CreateNotificationChannel(notificationChannel); } } notificationManager.Notify(0, mBuilder.Build()); } catch (Exception ex) { // } }
//public void Cancel(int id) //{ // var notificationManager = NotificationManagerCompat.From(_context); // notificationManager.CancelAll(); // notificationManager.Cancel(id); //} public void LocalNotification(string title, string body, DateTime time) { try { //long repeateDay = 1000 * 60 * 60 * 24; long repeateForMinute = 60000; // In milliseconds long totalMilliSeconds = (long)(time.ToUniversalTime() - _jan1st1970).TotalMilliseconds; if (totalMilliSeconds < JavaSystem.CurrentTimeMillis()) { totalMilliSeconds = totalMilliSeconds + repeateForMinute; } var intent = new Intent(_context, typeof(MainActivity)); intent.AddFlags(ActivityFlags.ClearTop); intent.PutExtra(title, body); var pendingIntent = PendingIntent.GetActivity(_context, 0, intent, PendingIntentFlags.OneShot); // Creating an Audio Attribute var alarmAttributes = new AudioAttributes.Builder() .SetContentType(AudioContentType.Sonification) .SetUsage(AudioUsageKind.Notification).Build(); _builder = new NotificationCompat.Builder(_context); _builder.SetSmallIcon(Resource.Drawable.app_logo); _builder.SetContentTitle(title) .SetAutoCancel(true) .SetContentTitle(title) .SetContentText(body) .SetChannelId(NOTIFICATION_CHANNEL_ID) .SetPriority((int)NotificationPriority.High) .SetVibrate(new long[0]) .SetDefaults((int)NotificationDefaults.Sound | (int)NotificationDefaults.Vibrate) .SetVisibility((int)NotificationVisibility.Public) .SetSmallIcon(Resource.Drawable.theWitcher); NotificationManager notificationManager = _context.GetSystemService(Context.NotificationService) as NotificationManager; if (global::Android.OS.Build.VERSION.SdkInt >= global::Android.OS.BuildVersionCodes.O) { NotificationImportance importance = global::Android.App.NotificationImportance.High; NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, title, importance); notificationChannel.EnableLights(true); notificationChannel.EnableVibration(true); notificationChannel.SetShowBadge(true); notificationChannel.Importance = NotificationImportance.High; notificationChannel.SetVibrationPattern(new long[] { 100, 200, 300, 400, 500, 400, 300, 200, 400 }); if (notificationManager != null) { _builder.SetChannelId(NOTIFICATION_CHANNEL_ID); notificationManager.CreateNotificationChannel(notificationChannel); } } notificationManager.Notify(0, _builder.Build()); } catch (Java.Lang.Exception ex) { } }
private static int MapToPriority(NotificationImportance importance) { //Map channel importance (Android 8+) to notification importance (android 7) int priority = NotificationCompat.PriorityDefault; switch (importance) { case NotificationImportance.Max: priority = NotificationCompat.PriorityMax; break; case NotificationImportance.High: priority = NotificationCompat.PriorityHigh; break; case NotificationImportance.Low: priority = NotificationCompat.PriorityLow; break; case NotificationImportance.Min: priority = NotificationCompat.PriorityMin; break; default: priority = NotificationCompat.PriorityDefault; break; } return(priority); }
public void On(string title, string body, int id) { Context context = Forms.Context; Intent intent = new Intent(context, typeof(MainActivity)); PendingIntent pendingIntent = PendingIntent.GetActivity(context, 0, intent, 0); //デフォルトの通知音を取得 Android.Net.Uri uri = RingtoneManager.GetDefaultUri(RingtoneType.Notification); string channelId = "TLExtension"; Notification.Builder builder = new Notification.Builder(context) .SetContentTitle(title) .SetContentText(body) .SetSmallIcon(Resource.Mipmap.icon); Notification notification = builder.Build(); NotificationManager notificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService); if (Build.VERSION.SdkInt >= Build.VERSION_CODES.O) { NotificationChannel notificationChannel = notificationManager.GetNotificationChannel(channelId); if (notificationChannel == null) { NotificationImportance importance = NotificationImportance.High; notificationChannel = new NotificationChannel(channelId, "Use in TL Extension", importance); notificationChannel.EnableLights(true); notificationChannel.EnableVibration(true); notificationManager.CreateNotificationChannel(notificationChannel); } builder = builder.SetChannelId(channelId); } notificationManager.Notify(id, notification); }
public NotificationChannelDeclaration(string id, string name, string description, NotificationImportance importance) { Id = id; Name = name; Description = description; Importance = importance; }
public override void OnReceive(Context context, Intent intent) { var extra = intent.GetStringExtra(LocalNotificationKey); var notification = DeserializeNotification(extra); //Generating notification var builder = new NotificationCompat.Builder(Application.Context) .SetContentTitle(notification.Title) .SetContentText(notification.Body) .SetSmallIcon(notification.IconId) .SetPriority((int)NotificationPriority.High) .SetVibrate(new long [0]) .SetChannelId(NOTIFICATION_CHANNEL_ID) .SetVisibility((int)NotificationVisibility.Public) .SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Ringtone)) .SetAutoCancel(true); NotificationManager cNotificationManager = Application.Context.GetSystemService(Context.NotificationService) as NotificationManager; if (Build.VERSION.SdkInt >= BuildVersionCodes.O) { NotificationImportance importance = NotificationImportance.High; NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, notification.Title, importance); notificationChannel.EnableLights(true); notificationChannel.EnableVibration(true); notificationChannel.SetShowBadge(true); notificationChannel.Importance = NotificationImportance.High; notificationChannel.SetVibrationPattern(new long[] { 100, 200, 300, 400, 500, 400, 300, 200, 400 }); if (cNotificationManager != null) { builder.SetChannelId(NOTIFICATION_CHANNEL_ID); cNotificationManager.CreateNotificationChannel(notificationChannel); } } var resultIntent = LocalNotificationService.GetLauncherActivity(); resultIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask); var stackBuilder = Android.Support.V4.App.TaskStackBuilder.Create(Application.Context); stackBuilder.AddNextIntent(resultIntent); Random random = new Random(); int randomNumber = random.Next(9999 - 1000) + 1000; var resultPendingIntent = stackBuilder.GetPendingIntent(randomNumber, (int)PendingIntentFlags.Immutable); builder.SetContentIntent(resultPendingIntent); // Sending notification var notificationManager = NotificationManagerCompat.From(Application.Context); notificationManager.Notify(randomNumber, builder.Build()); }
private void SendNotification(ChatMessage message) { var intent = new Intent(this, typeof(MainActivity)); intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop); var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot); var defaultSoundUri = RingtoneManager.GetDefaultUri(RingtoneType.Notification); notificationBuilder = new NotificationCompat.Builder(this) .SetSmallIcon(Resource.Drawable.icon) // Display this icon .SetContentTitle(message.UserName) // Set its title .SetContentText(message.Text) // The message to display. .SetAutoCancel(true) // Dismiss from the notif. area when clicked .SetSound(defaultSoundUri) // Sound of message .SetContentIntent(pendingIntent) .SetChannelId("10023") .SetPriority(1) .SetVisibility((int)NotificationVisibility.Public) .SetVibrate(new long[0]); if (App.IsActive) { var alarmAttributes = new AudioAttributes.Builder().SetContentType(AudioContentType.Sonification).SetUsage(AudioUsageKind.Notification).Build(); NotificationManager notificationManager = mContext.GetSystemService(Context.NotificationService) as NotificationManager; if (global::Android.OS.Build.VERSION.SdkInt >= global::Android.OS.BuildVersionCodes.O) { NotificationImportance importance = global::Android.App.NotificationImportance.High; NotificationChannel notificationChannels = new NotificationChannel("10023", message.UserName, importance); notificationChannels.EnableLights(true); notificationChannels.EnableVibration(true); notificationChannels.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification), alarmAttributes); notificationChannels.Importance = NotificationImportance.Max; notificationChannels.SetVibrationPattern(new long[] { 100, 100, 100, 300, 300, 100, 100, 100 }); if (notificationManager != null) { notificationBuilder.SetChannelId("10023"); notificationManager.CreateNotificationChannel(notificationChannels); } notificationManager.Notify(0, notificationBuilder.Build()); } else { var notificationManager1 = NotificationManager.FromContext(this); notificationManager1.Notify(idPush++, notificationBuilder.Build()); } //remove after debug var chatService = ViewModelLocator.Instance.Resolve(typeof(ChatService)) as IChatService; chatService.OnMessageReceived(message); } else { var chatService = ViewModelLocator.Instance.Resolve(typeof(ChatService)) as IChatService; chatService.OnMessageReceived(message); } }
public void CreateNotification(String title, String message) { try { var sound = global::Android.Net.Uri.Parse(ContentResolver.SchemeAndroidResource + "://" + mContext.PackageName + "/" + Resource.Raw.notification); // Creating an Audio Attribute var alarmAttributes = new AudioAttributes.Builder() .SetContentType(AudioContentType.Sonification) .SetUsage(AudioUsageKind.Notification).Build(); mBuilder = new NotificationCompat.Builder(mContext); mBuilder.SetSmallIcon(Resource.Drawable.notification_bg);//revisar mBuilder.SetContentTitle(title) .SetSound(sound) .SetAutoCancel(true) .SetContentTitle(title) .SetContentText(message) .SetChannelId(NOTIFICATION_CHANNEL_ID) .SetPriority((int)NotificationPriority.High) .SetVibrate(new long[0]) .SetDefaults((int)NotificationDefaults.Sound | (int)NotificationDefaults.Vibrate) .SetVisibility((int)NotificationVisibility.Public) .SetSmallIcon(Resource.Drawable.notification_bg); NotificationManager notificationManager = mContext.GetSystemService(Context.NotificationService) as NotificationManager; if (global::Android.OS.Build.VERSION.SdkInt >= global::Android.OS.BuildVersionCodes.O) { NotificationImportance importance = global::Android.App.NotificationImportance.High; NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, title, importance); notificationChannel.EnableLights(true); notificationChannel.EnableVibration(true); notificationChannel.SetSound(sound, alarmAttributes); notificationChannel.SetShowBadge(true); notificationChannel.Importance = NotificationImportance.High; notificationChannel.SetVibrationPattern(new long[] { 100, 200, 300, 400, 500, 400, 300, 200, 400 }); if (notificationManager != null) { mBuilder.SetChannelId(NOTIFICATION_CHANNEL_ID); notificationManager.CreateNotificationChannel(notificationChannel); } } notificationManager.Notify(0, mBuilder.Build()); } catch (Exception ex) { Log.Debug("Error: ", ex.Message); } }
public AndroidBackgroundServiceHostBuilder WithNotification(string channelId, string channelName, NotificationImportance notificationImportance, NotificationVisibility notificationVisibility, int icon, string title, string content) { ChannelId = channelId; ChannelName = channelName; NotificationImportance = notificationImportance; NotificationVisibility = notificationVisibility; Icon = icon; Title = title; Content = content; return(this); }
public void ReceiveNotification(String title, String message) { try { var intent = new Intent(mContext, typeof(MainActivity)); intent.AddFlags(ActivityFlags.ClearTop); intent.PutExtra(title, message); var pendingIntent = PendingIntent.GetActivity(mContext, 0, intent, PendingIntentFlags.OneShot); mBuilder = new NotificationCompat.Builder(mContext); mBuilder.SetSmallIcon(Resource.Drawable.xamarin_logo); mBuilder.SetContentTitle(title) .SetAutoCancel(true) .SetContentTitle(title) .SetContentText(message) .SetChannelId(NOTIFICATION_CHANNEL_ID) .SetPriority((int)NotificationPriority.High) .SetVibrate(new long[0]) .SetDefaults((int)NotificationDefaults.Sound | (int)NotificationDefaults.Vibrate) .SetVisibility((int)NotificationVisibility.Public) .SetSmallIcon(Resource.Drawable.xamarin_logo) .SetContentIntent(pendingIntent); NotificationManager notificationManager = mContext.GetSystemService(Context.NotificationService) as NotificationManager; if (global::Android.OS.Build.VERSION.SdkInt >= global::Android.OS.BuildVersionCodes.O) { NotificationImportance importance = global::Android.App.NotificationImportance.High; NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, title, importance); notificationChannel.EnableLights(true); notificationChannel.EnableVibration(true); notificationChannel.SetShowBadge(true); notificationChannel.Importance = NotificationImportance.High; notificationChannel.SetVibrationPattern(new long[] { 100, 200, 300, 400, 500, 400, 300, 200, 400 }); if (notificationManager != null) { mBuilder.SetChannelId(NOTIFICATION_CHANNEL_ID); notificationManager.CreateNotificationChannel(notificationChannel); } } notificationManager.Notify(0, mBuilder.Build()); } catch (Exception ex) { // } }
static void CreateChannel(string name, string description, bool sound, NotificationImportance importance = NotificationImportance.High) { CurrentChannel = new NotificationChannel(name.ToCamelCaseId().ToLower(), name, importance) { Description = description }; if (sound) { CurrentChannel.SetSound(GetSoundUri(), GetAudioAttributes()); } NotificationManager.CreateNotificationChannel(CurrentChannel); }
public void CreateChannel(string channelId, string channelName, NotificationImportance importance) { if (Build.VERSION.SdkInt < BuildVersionCodes.O) { // Notification channels are new in API 26 (and not a part of the // support library). There is no need to create a notification // channel on older versions of Android. return; } var channel = new NotificationChannel(channelId, channelName, importance); channel.LightColor = Color.Green; channel.LockscreenVisibility = NotificationVisibility.Public; Manager.CreateNotificationChannel(channel); }
private void createNotificationChannel() { if (Build.VERSION.SdkInt >= BuildVersionCodes.O) { string name = Constants.ChannelId; var description = "my channel desc"; NotificationImportance importance = NotificationImportance.High; var channel = new NotificationChannel(Constants.ChannelId, name, importance) { Description = description }; var notificationManager = (NotificationManager)Context.GetSystemService(NotificationService); notificationManager.CreateNotificationChannel(channel); } }
/// <summary> /// Create a topic to show the notification /// </summary> /// <returns></returns> NotificationChannel CreateChannel() { string channelIdOne = "com.my.fcm.test.app.mdenetchannel"; string nameOne = "Notification Hub"; string descriptionOne = "Notification Hub Test"; NotificationImportance importanceOne = Android.App.NotificationImportance.High; NotificationChannel channelOne = new NotificationChannel(channelIdOne, nameOne, importanceOne) { Description = descriptionOne }; channelOne.EnableLights(true); channelOne.EnableVibration(true); NotificationManager.CreateNotificationChannel(channelOne); return(channelOne); }
private void CreateNotificationChannel( string id, string name, string description, bool muted = false, NotificationImportance importance = NotificationImportance.Default) { var channel = new NotificationChannel(id, name, importance) { LightColor = Resource.Color.colorAccent, Description = description, }; if (muted) { channel.SetSound(null, null); } channel.EnableLights(true); NotifManager.CreateNotificationChannel(channel); }
private void CreateChannel() { string channelName = NotificationContext.Resources.GetString(Resource.String.channel_name); string channelDescription = NotificationContext.Resources.GetString(Resource.String.channel_description); NotificationImportance importance = NotificationImportance.High; if (_channel == null) { _channel = new NotificationChannel(_channelId, channelName, importance) { Description = channelDescription }; _channel.SetShowBadge(true); NotificationManager notificationManager = (NotificationManager)NotificationContext.GetSystemService(Context.NotificationService); notificationManager?.CreateNotificationChannel(_channel); } }
public void CreateNotification(string title, string message) { var alarmAttributes = new AudioAttributes.Builder() .SetContentType(AudioContentType.Sonification) .SetUsage(AudioUsageKind.Notification).Build(); mBuilder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID); mBuilder.SetSmallIcon(Resource.Mipmap.icon); mBuilder.SetContentTitle(title) .SetAutoCancel(true) .SetContentTitle(title) .SetContentText(message) .SetPriority((int)NotificationPriority.High) .SetVibrate(new long[0]) .SetDefaults((int)NotificationDefaults.Sound | (int)NotificationDefaults.Vibrate) .SetVisibility((int)NotificationVisibility.Public); NotificationManager notificationManager = mContext.GetSystemService(Context.NotificationService) as NotificationManager; if (global::Android.OS.Build.VERSION.SdkInt >= global::Android.OS.BuildVersionCodes.O) { NotificationImportance importance = global::Android.App.NotificationImportance.High; NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, title, importance); notificationChannel.EnableLights(true); notificationChannel.EnableVibration(true); notificationChannel.SetShowBadge(true); notificationChannel.Importance = NotificationImportance.High; notificationChannel.SetVibrationPattern(new long[] { 100, 200, 300, 400, 500, 400, 300, 200, 400 }); if (notificationManager != null) { mBuilder.SetChannelId(NOTIFICATION_CHANNEL_ID); notificationManager.CreateNotificationChannel(notificationChannel); } } notificationManager.Notify(0, mBuilder.Build()); }
public static void Configure( string name, string description, int iconResourceId, int transparentIconResourceId, Color transparentIconColor, bool sound, NotificationImportance importance = NotificationImportance.High, Action <Notification> onTapped = null) { Icon = new AndroidNotificationIcon(iconResourceId); TransparentIcon = new AndroidNotificationIcon(transparentIconResourceId); TransparentIconColor = transparentIconColor; OnTapped = onTapped; RegisterScheduledNotificationBroadcastReceiver(); if (OS.IsAtLeast(BuildVersionCodes.O)) { CreateChannel(name, description, sound, importance); } if (onTapped is not null) { UIRuntime.OnNewIntent.Handle(OnNewIntent); } }
private void sendNotification(string title, string message) { var intent = new Intent(this, typeof(MainActivity)); intent.AddFlags(ActivityFlags.SingleTop); var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot); notificationManager = (NotificationManager)GetSystemService(Context.NotificationService); if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O) { NotificationImportance importance = NotificationImportance.High; NotificationChannel notificationChannel = new NotificationChannel(newsChannelId, newsChannelDescription, importance); notificationChannel.EnableLights(true); notificationChannel.LightColor = Color.Red; notificationChannel.EnableVibration(true); notificationChannel.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification), new AudioAttributes.Builder() .SetContentType(AudioContentType.Sonification) .SetUsage(AudioUsageKind.Notification) .Build()); notificationManager.CreateNotificationChannel(notificationChannel); } Notification notification = new NotificationCompat.Builder(this, newsChannelId) .SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Mipmap.icon)) //newest projects use mipmaps and drawables, you can go to drawables too .SetSmallIcon(Resource.Mipmap.icon) .SetContentTitle(title) .SetContentText(message) .SetAutoCancel(true) .SetVisibility((int)NotificationVisibility.Private) .SetContentIntent(pendingIntent) .SetVibrate(vibrationPattern) .SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification)) .Build(); notificationManager.Notify(0, notification); // overrides old notification if it's still visible because it uses same Id }
private void CreateNotificationChannel() { if (Build.VERSION.SdkInt >= BuildVersionCodes.O) { NotificationImportance importance = NotificationImportance.High; NotificationChannel notificationChannel = new NotificationChannel(mChannelId, mChannelName, importance) { Description = mChannelDescription }; notificationChannel.EnableLights(true); notificationChannel.EnableVibration(true); notificationChannel.SetShowBadge(true); notificationChannel.Importance = NotificationImportance.High; notificationChannel.SetVibrationPattern(new long[] { 100, 200, 300, 400, 500, 400, 300, 200, 400 }); if (mManager != null) { mManager.CreateNotificationChannel(notificationChannel); } } mChannelInitialized = true; }
public void NotifyVersion26(Context context, Android.Content.Res.Resources res, Android.App.NotificationManager manager) { string channelName = "Secondary Channel"; NotificationImportance importance = NotificationImportance.High; NotificationChannel channel = new NotificationChannel(PrimaryChannel, channelName, importance); Uri path = Android.Net.Uri.Parse("android.resource://com.companyname.ctrip.driver/" + Resource.Raw.alert); AudioAttributes audioattribute = new AudioAttributes.Builder() .SetContentType(AudioContentType.Sonification) .SetUsage(AudioUsageKind.Notification) .Build(); channel.EnableLights(true); channel.EnableLights(true); channel.SetSound(path, audioattribute); channel.LockscreenVisibility = NotificationVisibility.Public; manager.CreateNotificationChannel(channel); Intent intent = new Intent(context, typeof(MainActivity)); intent.AddFlags(ActivityFlags.SingleTop); PendingIntent pendingIntent = PendingIntent.GetActivity(context, 0, intent, PendingIntentFlags.CancelCurrent); Notification.Builder builder = new Notification.Builder(context) .SetContentTitle("Ctrip Driver") .SetSmallIcon(Resource.Drawable.ic_location) .SetLargeIcon(BitmapFactory.DecodeResource(res, Resource.Drawable.iconimage)) .SetContentText("You have a new trip request") .SetChannelId(PrimaryChannel) .SetAutoCancel(true) .SetContentIntent(pendingIntent); manager.Notify(NotifyId, builder.Build()); }
public static void CreateNotificationChannel(Context ctx, string channelId, string channelName, string channelDescription, NotificationImportance importance) { // Create the NotificationChannel, but only on API 26+ because // the NotificationChannel class is new and not in the support library if (Build.VERSION.SdkInt >= BuildVersionCodes.O) { NotificationChannel channel = new NotificationChannel(channelId, channelName, importance); channel.Description = channelDescription; // Register the channel with the system; you can't change the importance // or other notification behaviors after this var notificationManager = NotificationManager.FromContext(ctx); notificationManager.CreateNotificationChannel(channel); // notificationManager.GetNotificationChannel(channelId).Importance } }
public void CreateNotification(String title, String message, IDictionary <string, string> data) { try { var intent = new Intent(mContext, typeof(MainActivity)); intent.AddFlags(ActivityFlags.ClearTop); intent.PutExtra(title, message); intent.PutExtra("notificationId", NOTIFICATION_CHANNEL_ID); foreach (var key in data.Keys) { if (key == "id") { intent.PutExtra("userMedId", data[key]); } else { intent.PutExtra(key, data[key]); } } var pendingIntent = PendingIntent.GetActivity(mContext, 0, intent, PendingIntentFlags.OneShot); // Creating an Audio Attribute var alarmAttributes = new AudioAttributes.Builder() .SetContentType(AudioContentType.Sonification) .SetUsage(AudioUsageKind.Notification).Build(); mBuilder = new NotificationCompat.Builder(mContext); mBuilder.SetSmallIcon(Resource.Drawable.clear_button_icon); mBuilder.SetContentTitle(title) .SetAutoCancel(true) .SetContentTitle(title) .SetContentText(message) .SetChannelId(NOTIFICATION_CHANNEL_ID) .SetPriority((int)NotificationPriority.High) .SetVibrate(new long[0]) .SetDefaults((int)NotificationDefaults.Sound | (int)NotificationDefaults.Vibrate) .SetVisibility((int)NotificationVisibility.Public) .SetSmallIcon(Resource.Drawable.close) .SetContentIntent(pendingIntent); if (data.ContainsKey("image")) { var urlString = data["image"].ToString(); var url = new URL(urlString); var connection = (HttpURLConnection)url.OpenConnection(); connection.DoInput = true; connection.Connect(); var input = connection.InputStream; var bitmap = BitmapFactory.DecodeStream(input); var style = new NotificationCompat.BigPictureStyle() .BigPicture(bitmap) .SetSummaryText(message); connection.Dispose(); mBuilder.SetStyle(style); } NotificationManager notificationManager = mContext.GetSystemService(Context.NotificationService) as NotificationManager; if (global::Android.OS.Build.VERSION.SdkInt >= global::Android.OS.BuildVersionCodes.O) { NotificationImportance importance = global::Android.App.NotificationImportance.High; NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, title, importance); notificationChannel.EnableLights(true); notificationChannel.EnableVibration(true); notificationChannel.SetShowBadge(true); notificationChannel.Importance = NotificationImportance.High; notificationChannel.SetVibrationPattern(new long[] { 100, 200, 300, 400, 500, 400, 300, 200, 400 }); if (notificationManager != null) { mBuilder.SetChannelId(NOTIFICATION_CHANNEL_ID); notificationManager.CreateNotificationChannel(notificationChannel); } } notificationManager.Notify(0, mBuilder.Build()); } catch (Exception ex) { // } }
/// <summary> /// Displays a notification, which opens the specified activity when clicked. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="channelId"></param> /// <param name="title"></param> /// <param name="text"></param> /// <param name="iconResourceId"></param> /// <param name="bigText"></param> /// <param name="priority"></param> /// <param name="autoCancel"></param> protected void ShowNotification <T>(string channelId, string title, string text, int iconResourceId, string bigText = default, NotificationImportance priority = NotificationImportance.Default, bool autoCancel = true) where T : Activity { try { var builder = new NotificationCompat.Builder(Context, channelId) .SetContentTitle(title).SetContentText(text).SetAutoCancel(autoCancel).SetPriority((int)priority).SetSmallIcon(iconResourceId); if (!string.IsNullOrEmpty(bigText)) { using (var style = new NotificationCompat.BigTextStyle()) { builder.SetStyle(style.BigText(bigText)); } } var intent = new Intent(Context, typeof(T)); var pending = PendingIntent.GetActivity(Context, 0, intent, PendingIntentFlags.UpdateCurrent); builder.SetContentIntent(pending); Logging.Log(LogType.Event, $"Firing Notification with Intent; Title: {title}; Text: {text}"); manager.Notify(1, builder.Build()); } catch (System.Exception e) { Logging.Log(LogType.Exception, $"Something went wrong processing the new notification. Exception message: {e.Message}; Exception Stacktrace: {e.StackTrace}"); throw; } }
/// <summary> /// Sets, the level of interruption of this notification channel. /// </summary> public NotificationChannelRequestBuilder WithImportance(NotificationImportance importance) { _channelRequest.Importance = importance; return(this); }
/// <summary> /// /// </summary> /// <param name="channelId"></param> /// <param name="channelName"></param> /// <param name="channelImportance"></param> public NotificationChannelProps(string channelId, string channelName, NotificationImportance channelImportance = NotificationImportance.Default) { NotificationChannelId = channelId; NotificationChannelName = channelName; NotificationChannelImportance = channelImportance; }
public string SendNotification() { try { var intent = new Intent(mContext, typeof(MainActivity)); intent.AddFlags(ActivityFlags.ClearTop); intent.PutExtra("Android Notification", "Notificaiton"); var pendingIntent = PendingIntent.GetActivity(mContext, 0, intent, PendingIntentFlags.OneShot); var sound = global::Android.Net.Uri.Parse(ContentResolver.SchemeAndroidResource + "://" + mContext.PackageName + "/" + Resource.Attribute.actionBarDivider); // Creating an Audio Attribute var alarmAttributes = new AudioAttributes.Builder() .SetContentType(AudioContentType.Sonification) .SetUsage(AudioUsageKind.Notification).Build(); mBuilder = new NotificationCompat.Builder(mContext); mBuilder.SetSmallIcon(Resource.Drawable.alerticon); mBuilder.SetContentTitle("Android Notification") .SetSound(sound) .SetAutoCancel(true) .SetContentTitle("Android Notification") .SetContentText("Notificaiton") .SetChannelId(NOTIFICATION_CHANNEL_ID) .SetPriority((int)NotificationPriority.High) .SetVibrate(new long[0]) .SetDefaults((int)NotificationDefaults.Sound | (int)NotificationDefaults.Vibrate) .SetVisibility((int)NotificationVisibility.Public) .SetSmallIcon(Resource.Drawable.alerticon) .SetContentIntent(pendingIntent); NotificationManager notificationManager = mContext.GetSystemService(Context.NotificationService) as NotificationManager; if (global::Android.OS.Build.VERSION.SdkInt >= global::Android.OS.BuildVersionCodes.O) { NotificationImportance importance = global::Android.App.NotificationImportance.High; NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notificaiton", importance); notificationChannel.EnableLights(true); notificationChannel.EnableVibration(true); notificationChannel.SetSound(sound, alarmAttributes); notificationChannel.SetShowBadge(true); notificationChannel.Importance = NotificationImportance.High; notificationChannel.SetVibrationPattern(new long[] { 100, 200, 300, 400, 500, 400, 300, 200, 400 }); if (notificationManager != null) { mBuilder.SetChannelId(NOTIFICATION_CHANNEL_ID); notificationManager.CreateNotificationChannel(notificationChannel); } } notificationManager.Notify(0, mBuilder.Build()); //MainActivity.ShowAlert(); AlertDialog.Builder alertDiag = new AlertDialog.Builder(MainActivity.contextForDialog); alertDiag.SetTitle("Notification"); alertDiag.SetMessage("Notification Generated"); EditText input = new EditText(MainActivity.contextForDialog); // Specify the type of input expected; this, for example, sets the input as a password, and will mask the text input.SetRawInputType(Android.Text.InputTypes.ClassText | Android.Text.InputTypes.NumberVariationPassword); alertDiag.SetView(input); alertDiag.SetPositiveButton("OK", (senderAlert, args) => { sendValue(input.Text.ToString()); //Toast.MakeText(MainActivity.contextForDialog, "Notification Opened", ToastLength.Short).Show(); }); alertDiag.SetNegativeButton("Cancel", (senderAlert, args) => { alertDiag.Dispose(); }); Dialog diag = alertDiag.Create(); diag.Show(); } catch (Exception ex) { // } return("123321"); }
/** * This is the method called to create the Notification */ public Notification setNotification(Context context, String title, String text, int icon) { if (notificationPendingIntent == null) { Intent notificationIntent = new Intent(context, Java.Lang.Class.FromType(typeof(MainActivity))); notificationIntent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop); notificationPendingIntent = PendingIntent.GetActivity(context, 0, notificationIntent, 0); } Notification notification; NotificationManager notificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService); // OREO if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O) { // Create the NotificationChannel, but only on API 26+ because // the NotificationChannel class is new and not in the support library Java.Lang.ICharSequence name = new Java.Lang.String("Permanent Notification"); NotificationImportance importance = NotificationImportance.Low; String CHANNEL_ID = "uk.ac.shef.oak.channel"; NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance); String description = "I would like to receive travel alerts and notifications for:"; channel.Description = description; NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, CHANNEL_ID); if (notificationManager != null) { notificationManager.CreateNotificationChannel(channel); } notification = notificationBuilder //the log is PNG file format with a transparent background .SetSmallIcon(icon) .SetColor(ContextCompat.GetColor(context, Resource.Color.colorAccent)) .SetContentTitle(title) .SetContentText(text) .SetContentIntent(notificationPendingIntent) .Build(); } else if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop) { notification = new NotificationCompat.Builder(context, "channel") // to be defined in the MainActivity of the app .SetSmallIcon(icon) .SetContentTitle(title) .SetContentText(text) .SetPriority(NotificationCompat.PriorityMin) .SetContentIntent(notificationPendingIntent).Build(); } else { notification = new NotificationCompat.Builder(context, "channel") // to be defined in the MainActivity of the app .SetSmallIcon(icon) .SetContentTitle(title) .SetContentText(text) .SetPriority(NotificationCompat.PriorityMin) .SetContentIntent(notificationPendingIntent).Build(); } return(notification); }
private void CreateNoti() { try { BigViews = new RemoteViews(PackageName, Resource.Layout.CustomNotificationLayout); SmallViews = new RemoteViews(PackageName, Resource.Layout.CustomNotificationSmallLayout); Intent notificationIntent = new Intent(this, typeof(SplashScreenActivity)); notificationIntent.SetAction(Intent.ActionMain); notificationIntent.AddCategory(Intent.CategoryLauncher); notificationIntent.PutExtra("isnoti", true); PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0, notificationIntent, PendingIntentFlags.UpdateCurrent); Intent previousIntent = new Intent(this, typeof(PlayerService)); previousIntent.SetAction(ActionRewind); PendingIntent ppreviousIntent = PendingIntent.GetService(this, 0, previousIntent, 0); Intent playIntent = new Intent(this, typeof(PlayerService)); playIntent.SetAction(ActionToggle); PendingIntent pplayIntent = PendingIntent.GetService(this, 0, playIntent, 0); Intent nextIntent = new Intent(this, typeof(PlayerService)); nextIntent.SetAction(ActionSkip); PendingIntent pnextIntent = PendingIntent.GetService(this, 0, nextIntent, 0); Intent closeIntent = new Intent(this, typeof(PlayerService)); closeIntent.SetAction(ActionStop); PendingIntent pcloseIntent = PendingIntent.GetService(this, 0, closeIntent, 0); Notification = new NotificationCompat.Builder(this, NotificationChannelId) .SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Mipmap.icon)) .SetContentTitle(AppSettings.ApplicationName) .SetPriority((int)NotificationPriority.Max) .SetContentIntent(pendingIntent) .SetSmallIcon(Resource.Drawable.icon_notification) .SetTicker(Constant.ArrayListPlay[Constant.PlayPos]?.Title) .SetChannelId(NotificationChannelId) .SetOngoing(true) .SetAutoCancel(true) .SetOnlyAlertOnce(true); NotificationChannel mChannel; if (Build.VERSION.SdkInt >= BuildVersionCodes.O) { NotificationImportance importance = NotificationImportance.Low; mChannel = new NotificationChannel(NotificationChannelId, AppSettings.ApplicationName, importance); MNotificationManager.CreateNotificationChannel(mChannel); MediaSessionCompat mMediaSession = new MediaSessionCompat(Application.Context, AppSettings.ApplicationName); mMediaSession.SetFlags(MediaSessionCompat.FlagHandlesMediaButtons | MediaSessionCompat.FlagHandlesTransportControls); Notification.SetStyle(new Android.Support.V4.Media.App.NotificationCompat.MediaStyle() .SetMediaSession(mMediaSession.SessionToken).SetShowCancelButton(true) .SetShowActionsInCompactView(0, 1, 2) .SetCancelButtonIntent(MediaButtonReceiver.BuildMediaButtonPendingIntent(Application.Context, PlaybackStateCompat.ActionStop))) .AddAction(new NotificationCompat.Action(Resource.Xml.ic_skip_previous, "Previous", ppreviousIntent)) .AddAction(new NotificationCompat.Action(Resource.Xml.ic_pause, "Pause", pplayIntent)) .AddAction(new NotificationCompat.Action(Resource.Xml.ic_skip_next, "Next", pnextIntent)) .AddAction(new NotificationCompat.Action(Resource.Drawable.ic_action_close, "Close", pcloseIntent)); } else { string songName = Methods.FunString.DecodeString(Constant.ArrayListPlay[Constant.PlayPos]?.Title); string genresName = Methods.FunString.DecodeString(Constant.ArrayListPlay[Constant.PlayPos]?.CategoryName) + " " + Application.Context.Resources.GetString(Resource.String.Lbl_Music); BigViews.SetOnClickPendingIntent(Resource.Id.imageView_noti_play, pplayIntent); BigViews.SetOnClickPendingIntent(Resource.Id.imageView_noti_next, pnextIntent); BigViews.SetOnClickPendingIntent(Resource.Id.imageView_noti_prev, ppreviousIntent); BigViews.SetOnClickPendingIntent(Resource.Id.imageView_noti_close, pcloseIntent); SmallViews.SetOnClickPendingIntent(Resource.Id.status_bar_collapse, pcloseIntent); BigViews.SetImageViewResource(Resource.Id.imageView_noti_play, Android.Resource.Drawable.IcMediaPause); BigViews.SetTextViewText(Resource.Id.textView_noti_name, songName); SmallViews.SetTextViewText(Resource.Id.status_bar_track_name, songName); BigViews.SetTextViewText(Resource.Id.textView_noti_artist, genresName); SmallViews.SetTextViewText(Resource.Id.status_bar_artist_name, genresName); BigViews.SetImageViewResource(Resource.Id.imageView_noti, Resource.Mipmap.icon); SmallViews.SetImageViewResource(Resource.Id.status_bar_album_art, Resource.Mipmap.icon); Notification.SetCustomContentView(SmallViews).SetCustomBigContentView(BigViews); } ShowNotification(); } catch (Exception e) { Console.WriteLine(e); } }
public void PostNotification(string title, string text, bool autoCancellable, NotificationImportance notificationImportance) { NotificationChannel notificationChannel = new NotificationChannel("livedisplaynotificationchannel", "LiveDisplay", notificationImportance); notificationManager.CreateNotificationChannel(notificationChannel); Notification.Builder builder = new Notification.Builder(Application.Context, "livedisplaynotificationchannel"); builder.SetContentTitle(title); builder.SetContentText(text); builder.SetAutoCancel(autoCancellable); builder.SetSmallIcon(Resource.Drawable.ic_stat_default_appicon); // RemoteInput remoteInput = new RemoteInput.Builder("test") //.SetLabel("Your inline response").Build(); // Intent intent = new Intent(Application.Context, Java.Lang.Class.FromType(typeof(SettingsActivity))); // PendingIntent pendingIntent = PendingIntent.GetActivity(Application.Context, 35, intent, PendingIntentFlags.UpdateCurrent); // Notification.Action.Builder action = new Notification.Action.Builder(Resource.Drawable.ic_stat_default_appicon, "Answer", pendingIntent).AddRemoteInput(remoteInput); // builder.AddAction(action.Build()); notificationManager.Notify(1, builder.Build()); }