Files
yavsc/BookAStar/BookAStar.Droid/Services/GcmRegistrationIntentService.cs
Paul Schneider 7175b0fb88 Merge branch 'vnext' of https://github.com/pazof/yavsc into vnext
* 'vnext' of https://github.com/pazof/yavsc: (38 commits)
fixe le premier démarrage
Corrige le mot de passe perdu d'un utilisateur au nom contenant des
espaces
traductions
fixe la reccupération du mot de passe
refabrique:
MEP index blogs
Specialized the book query notification
Updated the Licence validity
cleaned up log warnings
label event date
cleans the code
Adds support for SIREN exceptions to validation from the external
provider
layout
trads
refactoring
refactoring
Google maps: a map
image
implements an AccessDenied page
A better layout
...

# Conflicts:
#	Yavsc.Api/project.lock.json
#	Yavsc.Client/Yavsc.Client.csproj
#	Yavsc.Client/packages.config
#	sendmsg/Program.cs
2016-07-30 22:51:19 +02:00

113 lines
3.0 KiB
C#

using System;
using Android.App;
using Android.Content;
using Android.Util;
using System.Net;
using System.IO;
using Android.Gms.Gcm;
using Android.Gms.Gcm.Iid;
using Android.OS;
using Android;
namespace BookAStar.Droid
{
[Service(Exported = false)]
class GcmRegistrationIntentService : IntentService
{
static object locker = new object();
public GcmRegistrationIntentService() : base("RegistrationIntentService") {
}
static PowerManager.WakeLock sWakeLock;
static object LOCK = new object();
public override void OnCreate()
{
base.OnCreate();
sWakeLock = PowerManager.FromContext(this).NewWakeLock(WakeLockFlags.Partial,
"BookAStar");
sWakeLock.Acquire();
}
public override void OnDestroy()
{
base.OnDestroy();
sWakeLock.Release();
}
protected override void OnHandleIntent (Intent intent)
{
try
{
Log.Info ("RegistrationIntentService", "Calling InstanceID.GetToken");
lock (locker)
{
var instanceID = InstanceID.GetInstance(this);
#if DEBUG
// When debugging, and application data/cache is preserved,
// a previous instance comes from another application installation
// and the old registration against GCM fails,
// until one delete it.
try
{
instanceID.DeleteInstanceID();
}
catch(Exception ex)
{
Debug.WaitForDebugger();
Log.Debug("bas.GCM", ex.StackTrace.ToString());
}
#endif
var senderid = MainSettings.GoogleSenderId;
var token = instanceID.GetToken ( senderid,
GoogleCloudMessaging.InstanceIdScope, null);
Log.Info ("RegistrationIntentService", "GCM Registration Token: " + token);
SendRegistrationToAppServer (token);
Subscribe (token);
}
}
catch (WebException e) {
Log.Debug ("RegistrationIntentService", "Failed to get a registration token");
if (e.Response!=null)
using (var s = e.Response.GetResponseStream ()) {
using (var r = new StreamReader (s)) {
var t = r.ReadToEnd ();
Log.Debug("RegistrationIntentService",t);
}
}
return;
}
catch (Exception e)
{
Log.Error ("RegistrationIntentService", "Failed to get a registration token");
Log.Error ("RegistrationIntentService", e.Message);
return;
}
}
void SendRegistrationToAppServer (string token)
{
MainSettings.GoogleRegId = token;
}
void Subscribe (string token)
{
var pubSub = GcmPubSub.GetInstance(this);
pubSub.Subscribe(token, "/topics/global", null);
// TODO if a Activity is specified,
// and general annonces in this activity are accepted:
//
// pubSub.Subscribe(token, "/topics/jobs/"+ActivityCode, null);
}
}
}