c# - Xamarin.Android Notifications not working correctly -
using alarmmanager application creates notification in concrete time. faced problem, users' words notifications not appear or appear without sound (settings fine).
please tell can connected (users messengers applications create notifications without problem)?
can reason of use not notification.builder , notificationmanager, appcompat alternatives: notificationcompat.builder , notificationmanagercompat?
here code:
using system; using system.collections.generic; using system.linq; using system.text; using android.app; using android.content; using android.os; using android.runtime; using android.views; using android.widget; using android.support.v4.app; using system.threading; using android.support.v4.content; using me.leolin.shortcutbadger; using android.media; namespace weeklyapp { [broadcastreceiver] public class weeklyalarmreceiver : wakefulbroadcastreceiver { private notificationcompat.builder builder; private notificationmanagercompat manager; public static readonly int millisforpause = 1000; private settingsworker settingsworker; private dealsdbworker dealsdbworker; private int notificationvalue; private int soundvalue; private int badgevalue; private datetime currentdate; private string currentstringdate; private long currenttime; private list<task> currentdaytasks; private task notificationtask; private datetime startingunixtime = new datetime(1970, 1, 1); private string defaulttitletext; private string applicationname; private string notificationtext; private string dealnotificationtime; private int pendingintentid = 0; private int maxdealtextlength = 100; private string dealnotificationtext; private dealsdbworker dbworker; private datesworker datesworker; public static int notificationnumber = 0; public static list<int> taskids; private intent mainintent; private static int currentapiversion = (int)build.version.sdkint; public weeklyalarmreceiver() { defaulttitletext = application.context.getstring(resource.string.notificationdefaulttext); applicationname = application.context.getstring(resource.string.applicationname); } public override void onreceive(context context, intent intent) { settingsworker = new settingsworker(); dealsdbworker = new dealsdbworker(); mainintent = new intent(application.context, typeof(mainactivity)); mainintent = application.context.packagemanager.getlaunchintentforpackage(application.context.packagename); mainintent.setflags(activityflags.broughttofront | activityflags.cleartop); notificationvalue = settingsworker.getnotificationsvalue(); soundvalue = settingsworker.getsoundvalue(); badgevalue = settingsworker.getshowcountervalue(); if (builder == null) { builder = new notificationcompat.builder(context); } builder.setcontenttitle(applicationname); manager = notificationmanagercompat.from(application.context); if (currentapiversion >= 21) { builder.setsmallicon(resource.drawable.ic_notification); } else { builder.setsmallicon(resource.drawable.ic_launcher); } getcurrentnotificationtask(); if (taskids == null) { taskids = new list<int>(); } if (notificationtask != null) { createnotification(context); } } private void createnotification(context context) { mainintent.putextra("dealidtoshow", notificationtask.id); datetime date = datetime.parse(notificationtask.taskdate); pendingintent pendingintent = pendingintent.getactivity(context, notificationtask.id, mainintent, pendingintentflags.updatecurrent); builder.setcontentintent(pendingintent); getdealnotificationtime(); getdealnotificationtext(); notificationtext = string.format(defaulttitletext, dealnotificationtime, dealnotificationtext); builder.setcontenttext(notificationtext); builder.setstyle(new notificationcompat.bigtextstyle().bigtext(notificationtext)); builder.setautocancel(true); setnotification(); } private void setnotification() { if (soundvalue != 0) { builder.setvibrate(new long[] { 10, 1200 }); builder.setsound(ringtonemanager.getdefaulturi(ringtonetype.notification)); } if (notificationvalue != 0) { notificationnumber++; taskids.add(notificationtask.id); if (build.manufacturer.equals("xiaomi", stringcomparison.invariantcultureignorecase) && badgevalue != 0) { shortcutbadger.removecount(application.context); manager.cancel(101); if(notificationnumber != 0) { //shortcutbadger.applynotification(application.context, resultnotification, notificationnumber); shortcutbadger.applynotification(application.context, builder.build(), notificationnumber); } else { shortcutbadger.removecount(application.context); } } manager.notify(101, builder.build()); } } private void getdealnotificationtext() { int textlength = notificationtask.text.length; if (textlength <= maxdealtextlength) { dealnotificationtext = notificationtask.text; } else { dealnotificationtext = $"{notificationtask.text.substring(0, maxdealtextlength)}..."; } } private void getdealnotificationtime() { datetime resulttime = startingunixtime.addseconds(notificationtask.tasktime); string defaultstring = "{0}:{1}"; if (resulttime.minute < 10) { defaultstring = "{0}:0{1}"; } dealnotificationtime = string.format(defaultstring, resulttime.hour, resulttime.minute); } private task getcurrentnotificationtask() { currentdate = new datetime(datetime.now.year, datetime.now.month, datetime.now.day, datetime.now.hour, datetime.now.minute, 0); currentstringdate = currentdate.date.tostring().remove(10, 8); currentdaytasks = dealsdbworker.gettasks(currentstringdate, true); currenttime = (long)(currentdate - startingunixtime).totalseconds; if (currentdaytasks != null) { notificationtask = array.find(currentdaytasks.toarray(), (a => a.tasktime == currenttime)); if (notificationtask != null && (notificationtask.tasktime % 10) != 0) { notificationtask = null; } } else { notificationtask = null; } return notificationtask; } }
}
Comments
Post a Comment