Xamarin Android FCM Push

This commit is contained in:
mjjo
2018-03-27 18:07:09 +09:00
commit d16851e4eb
15 changed files with 635 additions and 0 deletions

102
MainActivity.cs Normal file
View File

@@ -0,0 +1,102 @@
using Android.App;
using Android.Gms.Common;
using Android.OS;
using Android.Util;
using Android.Widget;
using Firebase.Iid;
using Plugin.Badge;
using System.Net;
using System.Threading.Tasks;
namespace FCMTest
{
[Activity(Label = "FCMTest", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
const string TAG = "FCMTest MainActivity";
TextView msgText;
protected override void OnCreate(Bundle bundle)
{
Log.Debug(TAG, "google app id: " + Resource.String.google_app_id);
Log.Debug(TAG, "google api key: " + Resource.String.google_api_key);
if(Intent.Extras != null)
{
foreach(var key in Intent.Extras.KeySet())
{
var value = Intent.Extras.GetString(key);
Log.Debug(TAG, "Key: {0} Value: {1}", key, value);
}
//if(Intent.Extras.ContainsKey("badge") == true)
//{
// var badgeCnt = Intent.Extras.Get("badge");
// int iBadgeCnt;
// int.TryParse(badgeCnt.ToString(), out iBadgeCnt);
// CrossBadge.Current.SetBadge(iBadgeCnt);
//}
}
base.OnCreate(bundle);
SetContentView (Resource.Layout.Main);
msgText = FindViewById<TextView>(Resource.Id.msgText);
IsPlayServicesAvailable();
var refreshTokenButton = FindViewById<Button>(Resource.Id.refreshTokenButton);
refreshTokenButton.Click += delegate {
ConfigureFireBase();
};
var logTokenButton = FindViewById<Button>(Resource.Id.logTokenButton);
logTokenButton.Click += delegate {
Log.Debug(TAG, "InstanceID token: " + FirebaseInstanceId.Instance.Token);
};
}
private void ConfigureFireBase()
{
#if DEBUG
Task.Run(() => {
var instanceId = FirebaseInstanceId.Instance;
instanceId.DeleteInstanceId();
Log.Debug(TAG, "{0} {1}", instanceId?.Token?.ToString(), instanceId.GetToken(GetString(Resource.String.gcm_defaultSenderId), Firebase.Messaging.FirebaseMessaging.InstanceIdScope));
});
// For debug mode only - will accept the HTTPS certificate of Test/Dev server, as the HTTPS certificate is invalid /not trusted
ServicePointManager.ServerCertificateValidationCallback += (o, certificate, chain, errors) => true;
#endif
}
protected override void OnResume()
{
base.OnResume();
IsPlayServicesAvailable();
}
public bool IsPlayServicesAvailable()
{
int resultCode = GoogleApiAvailability.Instance.IsGooglePlayServicesAvailable(this);
if(resultCode != ConnectionResult.Success)
{
if(GoogleApiAvailability.Instance.IsUserResolvableError(resultCode))
msgText.Text = GoogleApiAvailability.Instance.GetErrorString(resultCode);
else
{
msgText.Text = "This device is not supported";
Finish();
}
return false;
}
else
{
msgText.Text = "Google Play Services is available.";
return true;
}
}
}
}