• Introducing XDA Computing: Discussion zones for Hardware, Software, and more!    Check it out!
  • Fill out your device list and let everyone know which phones you have!    Edit Your Device Inventory

Intermediate: OneSignal Push Notification Integration in Xamarin (Android)

Search This thread

muraliameakula

Senior Member
Dec 29, 2020
99
3
Overview
In this article, I will create a demo app along with the integration of OneSignal which is based on Cross platform Technology Xamarin. It provides messages that are "pushed" from a server and pop up on a user's device, even if the app is not in running state. They are a powerful re-engagement tool meant to provide actionable and timely information to subscribers.

OneSignal Service Introduction
OneSignal
is the fastest and most reliable service to send push notifications, in-app messages, and emails to your users on mobile and web, including content management platforms like WordPress and Shopify. Users can discover resources and training to implement One Signal’s SDKs.

Prerequisite
1. Xamarin Framework
2. Huawei phone
3. Visual Studio 2019
4. OneSignal Account

App Gallery Integration process
1. Sign In and Create or Choose a project on AppGallery Connect portal.
2640091000001768541.20210528081130.02308000849235721226714791248222:50520614041451:2800:0B781E976378795E0A4E817F6E8D89CC4B2089F4911484405C2D9F59507950A3.png


2. Navigate to Project settings and download the configuration file.
2640091000001768541.20210604092309.68018125839658117066137616790692:50520614041451:2800:B57AE517E48A9E0FB90338CFDE25745889CAC271A6C8726121A835F2F301D073.png


3. Navigate to General Information, and then provide Data Storage location.
2640091000001768541.20210604092322.13964253692541221053307232654412:50520614041451:2800:02069D3E897A0AA7DD3F9F6AF63EDEF7DCFF815CEFDB3E57D7206CC00129C518.png


OneSignal SDK Integration process

1. Choose Huawei Android (HMS) and provide app name.
2640091000001768541.20210604092349.84374796352124806981532190543414:50520614041451:2800:2D6B8E6976C971A722324175877C4BCE5D26AAE49A758C51A853BEB4A4B1796B.png


2. Choose Xamarin then click Next.
2640091000001768541.20210604092404.31466380694298246531076240741883:50520614041451:2800:6F6BC1DC2BFB918B001EB7FF8A14FF5BB45589F64F3A1F6B19EA2AC9EED23027.png


3. Copy your App Id.
2640091000001768541.20210604092414.41537192453142184662397253888918:50520614041451:2800:BD2F98ED7C219DA2F165E4B14B89E5CBD4B134500537E108E448920FFE7805F5.png


4. Create New Push message from One Signal’s Dashboard.
2640091000001768541.20210604092425.98583035932622772608722188642610:50520614041451:2800:29704FA43E45D0C77A0E16A388A1913BFF3BC1E0B0018269EF4A9EF3526DB304.png


5. Find Review Your Message tab, then click Send Message button.
2640091000001768541.20210604092437.57793539897555249327659419018364:50520614041451:2800:0B2C1042F050E3D708FF3B3FA0EC28E00459E23C25EB75FE5DC856D605CD8054.png


Installing the Huawei ML NuGet package

1. Navigate to Solution Explore > Project > Right Click > Manage NuGet Packages.
2640091000001768541.20210604092458.54894348841588143424267654030390:50520614041451:2800:C9C74890992D469685D08BAAB73D934755E50383ECAA2382B1718180BE5A437E.png


2. Search on Browser Com.OneSignal and Install the package.
2640091000001768541.20210604092508.81115736006086638973692948698341:50520614041451:2800:326F5834CBF49E2B993058730B9E95FB19830B7C1D50BBC6A4D52CA6CDBB4940.png


Xamarin App Development

1. Open Visual Studio 2019 and Create A New Project.
2640091000001768541.20210108035655.42042780351666805420633899500694:50520614041451:2800:72C15EC37CFFF0492006706A8CCB714F2287BFA010B7DEFE47E682F548D1E468.png


2. Configure Manifest file and add following permissions and tags.
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          android:versionCode="1"
          android:versionName="1.0"
          package="com.hms.onesignal">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" ></uses-sdk>
  <permission android:name="${applicationId}.permission.C2D_MESSAGE"
            android:protectionLevel="signature" />
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name"
  android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">

      <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="${applicationId}" />
          </intent-filter>
      </receiver>

  </application>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

3. Create Activity class with XML UI.

MainActivity.cs
This activity performs all the operation regarding Push notification.
Code:
using System;
using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Support.Design.Widget;
using Android.Support.V7.App;
using Android.Views;
using Android.Widget;

namespace OneSignalDemo
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            SetContentView(Resource.Layout.activity_main);

            Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
            SetSupportActionBar(toolbar);

        }

        private void setUpOneSignal()
        {
            OneSignal.Current.SetLogLevel(LOG_LEVEL.VERBOSE, LOG_LEVEL.NONE);

            OneSignal.Current.StartInit("83814abc-7aad-454a-9d20-34e3681efcd1")
             .InFocusDisplaying(OSInFocusDisplayOption.Notification)
             .EndInit();
        }

        public override bool OnCreateOptionsMenu(IMenu menu)
        {
            MenuInflater.Inflate(Resource.Menu.menu_main, menu);
            return true;
        }

        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            int id = item.ItemId;
            if (id == Resource.Id.action_settings)
            {
                return true;
            }

            return base.OnOptionsItemSelected(item);
        }

        private void FabOnClick(object sender, EventArgs eventArgs)
        {
            View view = (View) sender;
            Snackbar.Make(view, "Replace with your own action", Snackbar.LengthLong)
                .SetAction("Action", (Android.Views.View.IOnClickListener)null).Show();
        }

        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}

Xamarin App Build Result

1. Navigate to Build > Build Solution.
2640091000001768541.20210604091813.02242456449337010123810646740034:50520614041451:2800:D641DFFDD238B8674042290EF223932BB7BF1518826EFF717C08F7AB44A18EDE.png


2. Navigate to Solution Explore > Project > Right Click > Archive/View Archive to generate SHA-256 for build release and Click on Distribute.
2640091000001768541.20210604091825.98677970838679119986405248955050:50520614041451:2800:A39D50DF3A2A5D964FB954A80572AB517DC9F482BB9602B3C538A66072976860.png


3. Choose Archive > Distribute.
2640091000001768541.20210604091854.50030802429564679683057406496397:50520614041451:2800:A7CB34C6B5F973FD8BA9E2E933189A2603FF8AA0D5D9120CBED71FBA4CE62FDC.png


4. Choose Distribution Channel > Ad Hoc to sign apk.
2640091000001768541.20210604091908.50769452917549718794531450056576:50520614041451:2800:EC5F0A3D5909B9494BAEF0EF3FDAD11F599FE36B6D8A7EBCBAE8EF8122800009.png


5. Choose Demo keystore to release apk.
2640091000001768541.20210604091920.72128992365233872458305545214562:50520614041451:2800:172B43089CDE0E372F9DCA2E968A1906055CE6DC01D40D2D3271FE12D70B0D15.png


6. Build succeed and click Save.
2640091000001768541.20210604091935.60472313461010054226335709962772:50520614041451:2800:A3A73A5B8F1593A62BB4F5F2AB44A77FCCBCBB4B40FE21E48190171076355F59.png


7. Result.
2640091000001768541.20210604091747.32924292668672945807614883960077:50520614041451:2800:1F131767947421DEA947FAB6CE272447FCF4BDEF2D79456DAADAE06AC6B33D9F.png


Message Deliver statistics
2640091000001768541.20210604091719.08188869897835737834972361146869:50520614041451:2800:A54D5B62868222965F59FEA47A2FA9B46533FDAA4C5DC2E6C946FEA28B83F69C.png


Tips and Tricks
Notification Types-25
means OneSignal timed out waiting for a response from Huawei's HMS to get a push token. This is most likely due to another 3rd-party HMS push SDK or your own HmsMessageService getting this event instead of OneSignal.

Conclusion
In this article, we have learned how to integrate OneSignal Push Notification in Xamarin based Android application. Developer can send OneSignal’s Push Message to users for new updates or any other information.
Thanks for reading this article. Be sure to like and comment to this article, if you found it helpful. It means a lot to me.

References
OneSignal Docs: click here
OneSignal Developer: click here

Original Source