I have a client that wants to have a local notification that the user can not dismiss and will be there until the app is closed. I can get this to work in android but not in iOS. I am working in xamarin and have had to break out of the pcl to accomplish this and the notifications in general. Is this something that can be accomplished(does apple allow this)? Or is a widget extension what I am in need of creating?
You can present a local notification on top of your running app
by implementing IUNUserNotificationCenterDelegate and calling the WillPresentNotification completionHandler with the UNNotificationPresentationOptions.Alert option.
But you can not prevent the user from dismissing it, thus is would be cleared from the Notification Center.
I'm not sure what information you are trying to show in the "permanent" notification, but it sounds like a Today View Widget is something you should look at. The downside of course is Today widgets require that the user manually adds the widget to the Today view and depending upon your needs that they enabled it for lock screen usage.
Here is how you would do a local notification and make it present itself on top of your app....
Implement IUNUserNotificationCenterDelegate:
[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
completionHandler.DynamicInvoke( UNNotificationPresentationOptions.Alert );
}
Assign the UNUserNotificationCenter Delegate:
UNUserNotificationCenter.Current.Delegate = this;
Send a local notification:
var unAuthorizationOptions = UNAuthorizationOptions.Alert & UNAuthorizationOptions.Badge & UNAuthorizationOptions.Sound;
var authReply = await UNUserNotificationCenter.Current.RequestAuthorizationAsync(unAuthorizationOptions);
if (authReply.Item1 == true) // Xamarin.iOS does not label the tuple members ;-( (granted, error)
{
var content = new UNMutableNotificationContent
{
Title = "Local Notification",
Subtitle = "By SushiHangover",
Body = "StackOverflow rocks",
Badge = badgecount
};
var request = UNNotificationRequest.FromIdentifier("SushiHangover", content, null);
await UNUserNotificationCenter.Current.AddNotificationRequestAsync(request);
}
Related
I'm working on a Xamarin application and I'm very confused about making a local notification because it has a lot of details and channels things and others.
does anyone know how to make a simple notification
Even if you want to make it simple you need to know:
Android Notification is an element with several parts (title, description, logo...), that can be shown in the blocked screen or notification bar, that play a sound or vibrate, and that brings you to an application or make an action. You can control all of those elements.
To let the user the control of what the want to receive or not, devs must put their notification in channel, otherwise they won't work in Android 8.0 (Oreo) or above.
The steps are:
1-Create notification channel at the beggining of your Activity, inside OnCreate()
void CreateNotificationChannel()
{
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
//No need to create notification channel
return;
}
var name = Resources.GetString(Resource.String.channel_name);
var description = GetString(Resource.String.channel_description);
var channel = new NotificationChannel(CHANNEL_ID, name, NotificationImportance.Default)
{
Description = description
};
var notificationManager = (NotificationManager) GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
}
2-Create notification with all of its components
var builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.SetAutoCancel(true) // Dismiss the notification from the notification area when the user clicks on it
.SetContentIntent(new Intent()) // Just do nothing
.SetContentTitle("Simple notification") // Set the title
.SetNumber(count) // Display the count in the Content Info
.SetSmallIcon(Resource.Drawable.ic_stat_button_click) // This is the icon to display
.SetContentText("Description of the notification body"); // the message to display.
3-Put your notification in the channel
var notificationManager = NotificationManagerCompat.From(this);
notificationManager.Notify(NOTIFICATION_ID, builder.Build());
The whole example is taken from the official Xamarin docs, I really encourage you to take a few minutes to read the notifications docs, where you can learn about them in detail, I hope it helps!
Basically I want to set the icon on the push notification using Firebase. I am using their Firebase Admin .NET SDK. Which actually works just fine except for one minor detail, I cannot set the icon. Utilizing the class FirebaseAdmin.Messaging.Message then specifying the attribute Token then specifying the Notification object (which has three attributes):
Body
ImageUrl
Title
After setting those the push notification works fine, but the ImageUrl is an actual image withing the message not the icon. I want to set the icon. After look through the docs I noticed that Message has an attribute called Webpush of type WebpushConfig which that has an attribute Notification of type WebpushNotification and boom! there they are, a bunch of settings like Tile, Body, Vibrate and others, but the one I really want is in there Icon. Unfortunately setting the attributes for WebpushNotification do not seem to do anything. Here is my code.
public async Task Push(string token)
{
await messaging.SendAsync(new Message() {
Token = token,
Notification = new Notification() {
//Title = "This Title works",
//Body = "This body works",
//ImageUrl = "someUrlThatWorks"
},
Webpush = new WebpushConfig()
{
Notification = new WebpushNotification
{
Title = "This title does not work",
Body = "This body does not work",
Icon = "thisIconDoesNotWork.png"
}
}
});
}
Basically only the three attributes from FirebaseAdmin.Messaging.Notification work.
But the attributes from FirebaseAdmin.Messaging.WebpushNotification I cannot get to work, I just want the Icon from there.
Here are the Docs
Thank you.
add the following to AndroidManifest.xml file
<!-- Set custom default icon. This is used when no icon is set for incoming notification messages. -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/ic_stat_ic_notification" />
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
notification message. -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="#color/colorAccent" />
I'm writing a uwp app with notification listener and I'm trying to get the origin of a notification (like Google Chrome and the website it came from).
I tried using the AppInfo.DisplayInfo for a UserNotification but I can't get it to print the info, and I'm not sure if this is the right way to do this.
IReadOnlyList<UserNotification> notifs = await MainPage.listener.GetNotificationsAsync(Windows.UI.Notifications.NotificationKinds.Toast);
UserNotification n = notifs.Last();
var name = n.AppInfo.DisplayInfo.DisplayName;
I expected name to be the name of the app the notification came from but it seems to be empty or just not working. To be precise from a notification like this:
I want to extract the "Google Chrome" and / or "www.reddit.com".
Hopefully you have found a solution by now, but in case this helps anyone:
The notification doesn't know that your message is coming from a browser. The whole Windows notifications system doesn't take that into an account. Windows receives a UserNotification from an application, which in your case happens to be a browser. So your "origin" is the application "Google Chrome". Your best shot is to try and get the link from inside the notification itself, if it is inside the body somewhere.
I really think you should be able to find the link inside the text of the notification, but I would need more information on what exactly you are receiving to be sure. If you want to know how to read the text of the notification, do:
string appName = notif.AppInfo.DisplayInfo.DisplayName; //this will get you "Google Chrome"
NotificationBinding toastBinding = notif.Notification.Visual.GetBinding(KnownNotificationBindings.ToastGeneric);
if (toastBinding != null)
{
IReadOnlyList<AdaptiveNotificationText> textElements = toastBinding.GetTextElements();
string titleText = textElements.FirstOrDefault()?.Text;
string bodyText = string.Join("\n", textElements.Skip(1).Select(t => t.Text));
string website = ParseWebsiteFromText(bodyText); //my guess is the info you want is in here
}
From there you have to parse the information you want, if it's available.
If you want to know more, I suggest reading the documentation from here: https://learn.microsoft.com/en-us/windows/uwp/design/shell/tiles-and-notifications/notification-listener.
I'm currently using SlackConnector Repo https://github.com/noobot/SlackConnector. I've created a bot and it sends interactive messages to my chat. I would like to add functionality to my interactive buttons but upon clicking them i get this response. Darn – that didn’t work. Only Slack Apps can add interactive elements to messages. Manage your apps here: https://api.slack.com/apps/ So it looks like I need a request URL to get my past my current roadblock. Is there a way to Test the Interactive Message button locally?
List<SlackAttachment> attachments = new List<SlackAttachment>();
List<SlackAttachmentAction> actions = new List<SlackAttachmentAction>();
actions.Add(new SlackAttachmentAction
{
Name = "game",
Text = "chess",
Type = "button",
Value = "Chess"
});
actions.Add(new SlackAttachmentAction
{
Name = "game",
Text = "Falken's Maze",
Type = "button",
Value = "Maze"
});
actions.Add( new SlackAttachmentAction
{
Name = "game",
Text = "Thermonuclear War",
Type = "danger",
Value = "war"
});
attachments.Add(new SlackAttachment
{
Text = "Choose a game to play",
Fallback = "You are unable to choose a game",
CallbackId = "wopr_game",
ColorHex = "#3AA3E3",
Actions = actions
});
connection.Say(new BotMessage
{
ChatHub = chatHub,
Text = "Usage: !talk <user>",
Attachments = attachments
});
return Task.CompletedTask;
One thing I tried was I set the request URL to use a url generated from https://webhook.site/#/ and I still get the same response upon clicking
It looks to me like you have two problems.
You don't have a Slack app
Interactive Messages only work if you have a registered Slack app. That is why you got that error message. But you can easily create one. Just go here and click on "Create a new app". One reason you need one is that you need to tell Slack to which URL to send the request, after a user clicks a button.
Slack can't reach your local app
Slack's interactive messages will only work with apps that can be reached from the public Internet. So if you want to develop your app locally you need to open your web server to the Internet. There are many ways to do it, one secure way is to use a VPN tunnel service. One provider for this kind of service is ngrok, which is also recommended in the official Slack tutorials. I use it myself and it works great.
I have always been using Parse to manage my backend but switched to Azure due to the better integration with ASP.NET and Xamarin. In Parse I created a PFInstallation object and associated whatever property I needed to filter on before sending push notifications.
In Azure Mobile Services I am using the following code (in Xamarin iOS) to register the device:
public override void RegisteredForRemoteNotifications (UIApplication application, NSData deviceToken)
{
try {
var languageCode = NSLocale.PreferredLanguages [0];
ServiceClient.GetPush ().RegisterNativeAsync (deviceToken, new []{ languageCode }).ContinueWith ((t) => {
Console.WriteLine ("Device registration successful!");
});
} catch (Exception e) {
Console.WriteLine ("Could not register device for push notifications: {0}", e.Message);
}
}
I would like to have a way to remind my inactive users that they should play again. In Parse I just associated a "lastLoginDate" attribute to the PFInstallation object. How can I achieve the same with Azure Mobile Services?
As of now I am only able to filter based on the user's device language (because I save it as a tag) but not on the "lastLoginDate".
PS: in my backend I would like to have a code like this
var users = registrations.Where(u => u.LastLoginDate <= testDate);
SendNotificationToUsers(users);
UPDATE
I read on this page that you can use Tag Expressions to filter registrations but in the article it's not specified if you can use boolean expressions involving a date comparison. Any help on this?
Thank you.
One approach would be to have the device register with a tag with the current date each time the user opens the app. So today I use the app, I register my token for the tag "last-use-2015-03-02". Tomorrow if I use the app again I will reregister with the tag "last-use-2015-03-03", replacing the device registration for 2015-03-02.
Then each day have a scheduler that sends a message to the tag for a week ago (for example). So a week from now on March 9, I can send a notification to all devices with the tag "last-use-2015-03-02" reminding them to come back.