Skip to content

Instantly share code, notes, and snippets.

@willsam100
Created March 10, 2019 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willsam100/d4775b42a09394fed25069ca18cfcd49 to your computer and use it in GitHub Desktop.
Save willsam100/d4775b42a09394fed25069ca18cfcd49 to your computer and use it in GitHub Desktop.
FCM MainActivity OnMessageReceived with circular dependencies F#
type NotificationHandler(context:Context) =
let createNotificationChannel () =
let CHANNEL_ID = "my_channel_01";// The id of the channel.
let name = "FcmChannel"
let importance = Android.App.NotificationImportance.High
let mChannel =
new NotificationChannel(CHANNEL_ID, name, importance,
LockscreenVisibility = NotificationVisibility.Public)
mChannel.EnableVibration true
mChannel.EnableLights true
(CHANNEL_ID, mChannel)
member this.ShowNotification (title:string) (body:string) =
Debug.WriteLine "Showing notification"
let intent = new Intent(context, typeof<MainActivity>)
let pendingIntent = PendingIntent.GetActivity(context, 0, intent, PendingIntentFlags.UpdateCurrent)
let notification =
if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O) then
let (CHANNEL_ID, mChannel) = createNotificationChannel()
NotificationManager.FromContext(context).CreateNotificationChannel mChannel
(new Android.App.Notification.Builder(context, CHANNEL_ID))
.SetSmallIcon(Resources.Drawable.icon)
.SetContentTitle(title)
.SetContentText(body)
.SetChannelId(CHANNEL_ID)
.SetContentIntent(pendingIntent)
.SetAutoCancel(true)
.Build();
else
(new App.NotificationCompat.Builder(context))
.SetPriority(App.NotificationCompat.PriorityDefault)
.SetSmallIcon(Resources.Drawable.icon)
.SetContentTitle(title)
.SetContentText(body)
.SetContentIntent(pendingIntent)
.SetAutoCancel(true)
.Build();
NotificationManager.FromContext(context).Notify(1, notification);
interface ShowNotification with
member this.ShowNotification title body =
this.ShowNotification title body
and [<Service; IntentFilter([| "com.google.firebase.MESSAGING_EVENT" |])>]
MyFcmListenerService() as this =
inherit FirebaseMessagingService()
let dictToMap (dic : System.Collections.Generic.IDictionary<_,_>) =
dic
|> Seq.map (|KeyValue|)
|> Map.ofSeq
let (|HasForegroundUI|InBackground|) (activity: Activity) =
match activity with
| :? MainActivity as mainActivity ->
match mainActivity.MainPage () with
| Some mainPage ->
match mainPage with
| :? Pushy.MainPage as mainPage -> HasForegroundUI mainPage
| _ -> InBackground
| None -> InBackground
| _ -> InBackground
// We must show on the UI and return response if successful. If we do a check first, the
// user could change the state of the application, leading to a missed notification.
let handleNotification title body =
match CrossCurrentActivity.Current.Activity with
| HasForegroundUI mainPage -> mainPage.HandleMessage title body
| InBackground ->
Debug.WriteLine "In background, showing notification"
let notifHandler = NotificationHandler(this)
notifHandler.ShowNotification title body
override this.OnMessageReceived(message: RemoteMessage) =
if message.GetNotification() <> null then
let notif = message.GetNotification()
handleNotification notif.Title notif.Body
let data = dictToMap message.Data
if data.ContainsKey "title" && data.ContainsKey "body" then
let title, body = (data.["title"], data.["body"])
handleNotification title body
data |> Map.iter (fun key value -> Debug.WriteLine <| sprintf "%s:%s" key value)
and [<Activity (Label = "Pushy.Droid", Icon = "@drawable/icon", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = (ConfigChanges.ScreenSize ||| ConfigChanges.Orientation))>]
MainActivity() as this =
inherit FormsAppCompatActivity()
let isPlayServicesAvailable () =
let resultCode =
GoogleApiAvailability.Instance.IsGooglePlayServicesAvailable(this)
if resultCode = ConnectionResult.Success then
HasGooglePlayServices
else
if GoogleApiAvailability.Instance.IsUserResolvableError resultCode then
GoogleApiAvailability.Instance.GetErrorString(resultCode) |> RequiresUser
else NoGooglePlayServices
let mutable formsApp: Pushy.App option = None
member this.MainPage(): Xamarin.Forms.Page Option =
formsApp |> Option.map (fun x -> x.MainPage)
override this.OnCreate (bundle: Bundle) =
FormsAppCompatActivity.TabLayoutResource <- Resources.Layout.Tabbar
FormsAppCompatActivity.ToolbarResource <- Resources.Layout.Toolbar
Debug.WriteLine <| sprintf "Token: %s" FirebaseInstanceId.Instance.Token
base.OnCreate (bundle)
CrossCurrentActivity.Current.Init(this, bundle)
Xamarin.Essentials.Platform.Init(this, bundle)
Xamarin.Forms.Forms.Init (this, bundle)
let app = new Pushy.App (isPlayServicesAvailable, NotificationHandler this)
formsApp <- Some app
this.LoadApplication app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment