Broadcast and Receiver to access the method in the Service

MikeDennisMikeDennis USMember ✭✭
edited February 2016 in Xamarin.Forms

Is it good to use Broadcast Receiver?

Problem: I need to send the list of topics to SubscribeToTopics() from the XF from the PCL project.

@AdamP - any guess on this
Note: GCM Notification works.

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Android.App;
using Android.Content;
using Android.Gms.Gcm;
using Android.Gms.Gcm.Iid;
using Android.OS;
using Android.Preferences;
using Android.Provider;
using Android.Runtime;
using Android.Support.V4.Content;
using Online.Radio.Core.Helpers;
using Online.Radio.Droid.Helpers;
using Android.Util;
using Android.Views;
using Android.Widget;
using Online.Forms.Shared.Plugins;
using Xamarin;
[assembly: Xamarin.Forms.Dependency(typeof(GcmService))]
namespace Online.Radio.Droid.Helpers
{
[Service(Exported = false)]
class GcmService : IntentService, IPushNotificationUserTopics
{
static readonly object Locker = new object();
private string _token;
public GcmService() : base("GcmService") { }

                                            protected override void OnHandleIntent(Intent intent)
                                            {
                                                FetchToken();
                                            }


                                            private string FetchToken()
                                            {
                                                try
                                                {
                                                    _token = InstanceID.GetInstance(this).GetToken("12345678", GoogleCloudMessaging.InstanceIdScope, null);
                                                    SubscribeToTopics(new List<string>());
                                                    GetSharedPreferences(Resources.GetString(Resource.String.settings_key), FileCreationMode.Private).Edit().PutString(Resources.GetString(Resource.String.gcm_token_key), _token).Commit();
                                                    return _token;
                                                }
                                                catch (Exception e)
                                                {
                                                    Console.WriteLine(e);
                                                    Log.Error("GcmService", "Failed to get a registration token. Exception message=" + e.Message);
                                                    return null;

                                                }
                                            }

                                            private ISet<string> _subscribedTopics = new HashSet<string>();
                                            public void SubscribeToTopics(IList<string> newTopics)
                                            {
                                                new Thread(() =>
                                                {
                                                    if (string.IsNullOrEmpty(_token))
                                                        FetchToken();

                                                    newTopics.Add("testing");
                                                    if (_subscribedTopics.SetEquals(newTopics))
                                                        return;

                                                    var pubSub = GcmPubSub.GetInstance(this);


                                                    try
                                                    {
                                                        foreach (var topic in _subscribedTopics)
                                                            pubSub.Unsubscribe(_token, "/topics/" + topic);
                                                        foreach (var topic in newTopics)
                                                            pubSub.Subscribe(_token, "/topics/" + topic, null);
                                                        _subscribedTopics = new HashSet<string>(newTopics);
                                                        //newTopics.Remove("testing");
                                                    }
                                                    catch (Exception ex)
                                                    {
                                                        Log.Error("GcmService", ex.Message);
                                                        Insights.Report(ex, Insights.Severity.Critical);
                                                    }
                                                //   Thread.Sleep(1000*60);
                                                }).Start();
                                            }
                                            public void UnSubscribeToTopics(IList<string> newTopics)
                                            {
                                                new Thread(() =>
                                                {
                                                    if (string.IsNullOrEmpty(_token))
                                                        FetchToken();
                                                    var pubSub = GcmPubSub.GetInstance(this);
                                                    try
                                                    {
                                                        foreach (var topic in newTopics)
                                                            pubSub.Unsubscribe(_token, "/topics/" + topic);

                                                    }
                                                    catch (Exception ex)
                                                    {
                                                        Log.Error("GcmService", ex.Message);
                                                        Insights.Report(ex, Insights.Severity.Critical);
                                                    }

                                                }).Start();
                                            }


                                            void IPushNotificationUserGuid.UpdateTopic(string userTopic)
                                            {
                                                string testHitPoint = userGuid;
                                            }
                                        }
                                    }

Answers

  • MikeDennisMikeDennis USMember ✭✭

    Created a dependency Class with the following code, where in I will be calling this method to broadcast from the PCL project, currently able to hit the breakpoint till the sendBroadCast method in the dependency service. but Receiver doesn't gets any data

    public void NewTopic(string userTopic)
    {
    var context = Xamarin.Forms.Forms.Context;
    var intent = new Intent("TopicSubscribe");
    intent.PutExtra("topic", userTopic);

           context.SendBroadcast(intent);
    
        }
    
  • MikeDennisMikeDennis USMember ✭✭

    @AdamP any guess on this problem?

  • AdamPAdamP AUUniversity ✭✭✭✭✭

    @MikeDennis - apologies, will look at it soon (still on 60hrs+ per week xamarin forms projects). Will need to do some further research into it, its been a while since I implemented push notifications on Xamarin.Android. Will hopefully get back to you within 6 or so hours.

  • AdamPAdamP AUUniversity ✭✭✭✭✭

    @MikeDennis - I am not too refreshed on this but if you are using a GCMService and I am assuming you are using a GCM Receiver, which I can't see the code for, then you need to send a message via GCM and not SendBroadcast which I believe is local. (I could be completely wrong about this so please feel free to challenge anything I am saying, I was hoping someone else with more knowledge on this subject might stop by).

    If you have a look at this: https://www.pubnub.com/blog/2015-06-24-sending-receiving-android-push-notifications-with-gcm-google-cloud-messaging/

    It also includes information about permissions etc to make sure you are on the right track.

    The way I architect Push Notifications is to send a message to an API, that will then send a message via GCM. However I use Azure Notification Hub so I send a message there, that then sends it out via GCM, or any other network for other platforms.

    If this isn't really helping you, I would post your question in the Xamarin Android forum section as this is certainly an Android question.

Sign In or Register to comment.