A blog about Xamarin.Forms, .NET MAUI, .NET MAUI Handler, Xamarin.iOS, Xamarin.Android, Custom Renderer, Xamarin Custom Control, Push Notification, Play Video From URI, Data Bindings. Xamarin Calendar
Tuesday, 18 October 2022
Thursday, 13 October 2022
Implement Push Notification In .NET MAUI (iOS)
Creation Of Certificates & Profiles
Certificate Create Development Certificate In Your Apple Developer Account.
Now Download GoogleService-Info.plist file.
Now Install Xamarin.Firebase.iOS.CloudMessaging Plugin.
Once Certificated Created, Download it and Install In Your Mac (Double Tap On Downloaded Profile)
Identifiers (Bundle Identifier)
Create Bundle Identifier Of Your App.
Devices : Add Your Testing Device in Devices Section.
Profiles (Provisional Profile)
Create Development / Distribution Provisional Profile
Once Profile Created, Download it and Install In Your Mac (Double Tap On Downloaded Profile)
Keys: Add APNs Key In Keys Option. Used for Push Notification
Create APNs Key and Download It and Save this file in some safe place. As You Can Only Download it once.
Adding App To Firebase Console
Create / Open Existing Project In Firebase Console.
Now Click On Register app Option To Register App.
Now Download GoogleService-Info.plist file.
Now Select Project Setting Option from Project Overview.
Now Select Cloud Messaging Tab & Select Your Newly Created App.
Here You Need To Add APN Auth Key That You Created From Apple Developer Portal.
Click on Upload Button To Upload APNs Auth Key.
Add Key ID : You will find Key From Apple Developer Portal Keys Options.
Now Click On Keys that you have created you will find Key ID.
Add Team ID: You Will Find Team ID from Membership Detail Tab of Apple Developer Account.
All Setup Is Ready Now Lets Implement that In .NET MAUI Project.
Implementing Push Notification In .NET MAUI Project
Create A New .NET MAUI Project With Sample Bundle Identifier that You had created
In Apple Developer Account.
Or You Can Edit Existing Project Bunder Identifier (App Identifier)
Now Right Click On Your Project Dependencies/net7.0-ios or net 6.0-ios Option & Click On Manage NuGet Packages.
Now Add Following Code in Your AppDelegate.cs File of iOS Platform.
AppDelegate.cs
using Firebase.CloudMessaging; using Firebase.Core; using Foundation; using PushNotificationDemoMAUI.Platforms.iOS; using UIKit; using UserNotifications; namespace PushNotificationDemoMAUI; [Register("AppDelegate")] public class AppDelegate : MauiUIApplicationDelegate, IUNUserNotificationCenterDelegate, IMessagingDelegate { public object completionhandler { get; private set; } protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp(); public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions) { Firebase.Core.App.Configure(new Options("AddgoogleAppID","AddgcmSenderID") { ApiKey = "AddAPIKey", ClientId = "ClientID", BundleId = "com.companyname.pushnotificationdemomaui", ProjectId = "Your ProjectID" }); //Adding GoogleService-Info.plist file and calling Firebase.Core.App.Configure() is not working // In .NET MAUI due to that I Manually Pass all Above Info. // Firebase.Core.App.Configure(); if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0)) { // iOS 10 or later var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound; UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) => { Console.WriteLine(granted); }); // This Delete I used To Display Always Notification Even App Is Open UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate(); Messaging.SharedInstance.AutoInitEnabled = true; Messaging.SharedInstance.Delegate = this; } UIApplication.SharedApplication.RegisterForRemoteNotifications(); return base.FinishedLaunching(application, launchOptions); } [Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")] public void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler) { // Here Handle Notification Navigation WhenNotification Is REceived } [Export("messaging:didReceiveRegistrationToken:")] public void DidReceiveRegistrationToken(Messaging messaging, string fcmToken) { if (Preferences.ContainsKey("DeviceToken")) { Preferences.Remove("DeviceToken"); } Preferences.Set("DeviceToken", fcmToken); App.Current.MainPage.DisplayAlert("Ok",fcmToken,"OK"); } }
GoogleAppID : You Will Get From Project Setting / General Tab (Firebase Console)
GcmSenderID: You Will Get From the GoogleService-Info.plist File
ApiKey: You Will Get From the GoogleService-Info.plist File
Client ID : You Will Get From the GoogleService-Info.plist File
BundleID : Your Project App Identifier
ProjectID: You will get from the Project Setting / General Tab (Firebase Console)
UserNotificationCenterDelegate.cs
using UserNotifications; namespace PushNotificationDemoMAUI.Platforms.iOS { public class UserNotificationCenterDelegate : UNUserNotificationCenterDelegate { public UserNotificationCenterDelegate() { } public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification,Action<UNNotificationPresentationOptions> completionHandler) { base.WillPresentNotification(center, notification, completionHandler); completionHandler(UNNotificationPresentationOptions.Alert); } } }
Now Create Entitlements.plist In Your Project Platforms/iOS
In Entitlements.plist file Add aps-environment as a key & development as a value.
Entitlements.plist
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>aps-environment</key> <string>development</string> </dict> </plist>
Now All Setup Is Ready You can send push notification to device token.
Send Test Notification From Firebase Console.
Create New Campaign In Firebase Console.
Add Title & Message Then Click On Send test Message Button.
Now Add FCM Registration Token that is generated on First App Launch and Then Click on Test Button to send notification.
Sunday, 9 October 2022
Wednesday, 5 October 2022
Subscribe to:
Posts (Atom)
Popular Posts
-
<Label Text = "{Binding Date,StringFormat='{0:MM/dd/yy}'}" /> Output: 05/25/2019 Date Formats: DateTime....
-
RatingBar Control- is a cross-platform plugin used for selecting the rating. How To Use Available on NuGet: https://www.nuget.org/...
-
Youtube Video: Packages Required Install sqlite-net-pcl Package in your .Net MAUI Project. Create EmployeeModel with few properties ...