Search This Blog

Wednesday 12 September 2018

Push Notification in Xamarin Form using OneSignal

What is OneSignal
OneSignal is a high volume and reliable push notification service for website and mobile application.

Add NuGet Package
From the solution explorer right click on your project and select Manage NuGet Package Option
then search for the OneSignal and install it.


Android/iOS SDK Setup
  • A OneSignal Account, if you do not already have one
  • Your OneSignal App ID, available in Keys & IDs
  • A Google/Firebase Server API Key
  • An iOS Push Certificate. Generate one here using our provisionator tool.
  • An iOS device (iPhone, iPad, iPod Touch) to test on. The Xcode simulator doesn't support push notifications so you must test on a real device.
  • A Mac with a new version of Xcode
Login With Google or Create Account in OneSignal Then Click on Add App Button To Add new App
Then Select the platform to configure.


For Android Google Freebase Server Key required that you will get from Google freebase console.


For iOS Generate the iOS Push Certifcate and upload it.


From the Keys & IDs you will get the OneSignal APP ID

Add the following code to your App.xaml.cs
using Com.OneSignal;

public App()
{
 InitializeComponent();
 MainPage = new OneSignalXamarinFormsExamplePage();

 OneSignal.Current.StartInit("YOUR_ONESIGNAL_APP_ID")
                 .EndInit();



//when you call the StartInit() method your device player id will register with

// the one signal you can check that from the www.onesignal.com website.

}






UserIDs (getting onesignal playerID)
The OneSignal PlayerID is a UUID (Unique Universal Identifier) that OneSignal gives all devices that use your mobile app or subscribe to your site.

Com.OneSignal.OneSignal.Current.IdsAvailable(getID); //Handle this method
private void getID(string playerID, string pushToken)
{
  // write logic for store playerID in the db
}
 you can write the logic like whenever the app start at that time player id will be stored in database.


Android Setup
<application ....>
 <receiver android:name="com.onesignal.GcmBroadcastReceiver"
           android:permission="com.google.android.c2dm.permission.SEND" >
   <intent-filter>
     <action android:name="com.google.android.c2dm.intent.RECEIVE" />
     <category android:name="${manifestApplicationId}" />
   </intent-filter>
 </receiver>
</application>

Replace all 3 of instances of ${manifestApplicationId} with your package name in AndroidManifest.xml.

iOS Setup
In your application's Info.plist, verify your Bundle Identifier matches you App Settings' Bundle ID,
Enable Background Modes, allow Remote notifications.
Sending push notification to registered device
var viewButton = new Dictionary<string, string>();
viewButton.Add("id", "openCompendium"); viewButton.Add("text", "View"); viewButton.Add("icon", "mail24"); //viewButton this button will display when the notification received in mobile. string postedBy = MasterDetailView.selectedEmail; var notification = new Dictionary<string, object>(); notification["headings"] = new Dictionary<string, string>() { { "en", "New Post Added" } }; notification["contents"] = new Dictionary<string, string>() { { "en", "Xyz Has added a new post for you" } }; notification["include_player_ids"] = new List<string>() { playerID }; //here you can specify the player id notification["buttons"] = new List<object> { viewButton }; // here you can add multiple button //by seperated by comma notification["small_icon"] = "icon20"; notification["priority"] = 10; notification["send_after"] = System.DateTime.Now.ToUniversalTime().AddSeconds(1).ToString("U"); Com.OneSignal.OneSignal.Current.PostNotification(notification);

Handle action button click in one signal

Com.OneSignal.OneSignal.Current.StartInit("OnesigalAppID"). HandleNotificationOpened(HandleNotificationOpened).EndInit(); private void HandleNotificationOpened(OSNotificationOpenedResult result) { string actionID = result.action.actionID;
// whatever the action id register while sending notification that will return here. }




1 comment:

  1. when app is open only then this function is calling otherwise not working

    ReplyDelete

Popular Posts