/// <summary> /// Handle push intent from <see cref="ParsePushBroadcastReceiver"/>. /// </summary> /// <param name="intent">The intent to be handled.</param> protected override void OnHandleIntent(Intent intent) { Task task = Task.FromResult(0); try { // Assume only GCM intent is received here. switch (intent.Action) { case ParsePushBroadcastReceiver.ActionGcmRegisterResponse: task = GcmRegistrar.GetInstance().HandleRegistrationIntentAsync(intent); break; case ParsePushBroadcastReceiver.ActionGcmReceive: if (ManifestInfo.HasPermissionForGCM()) { ParsePush.parsePushNotificationReceived.Invoke(ParseInstallation.CurrentInstallation, new ParsePushNotificationEventArgs(ParsePush.PushJson(intent))); } break; default: // TODO (hallucinogen): Prints error that we don't support other intent. break; } // Wait for its completion with timeout. task.Wait(IntentServiceHandlerTimeout); } finally { ParseWakefulHelper.CompleteWakefulIntent(intent); } }
public void Initialize() { if (ManifestInfo.HasPermissionForGCM()) { GcmRegistrar.GetInstance().Register(); } }
public void Register() { ParseInstallation installation = ParseInstallation.CurrentInstallation; lock (mutex) { if (installation.DeviceToken == null && request == null) { var metadata = ManifestInfo.GetApplicationMetaData(); object senderIdExtra = null; if (metadata != null) { senderIdExtra = metadata.Get(ExtraSenderId); } string senderIds = ParseGcmSenderId; if (senderIdExtra != null) { string senderId = getActualSenderIdFromExtra(senderIdExtra); if (senderId != null) { senderIds += "," + senderId; } else { Android.Util.Log.Error("parse.GcmRegistrar", "Found " + ExtraSenderId + " <meta-data> element with value \"" + senderIdExtra.ToString() + "\", but the value is missing the expected \"id:\" prefix"); } } request = Request.CreateAndSend(this.context, senderIds); } } }
/// <summary> /// Throws exception if the <c>AndroidManifest.xml</c> file doesn't contain any the required Service, BroadcastReceiver and permissions /// to receive GCM push notifications. /// </summary> public static bool HasPermissionForGCM() { bool hasParsePushService = getServiceInfo(typeof(ParsePushService)) != null; bool hasApplicationPermission = hasPermissions( "android.permission.INTERNET", "android.permission.ACCESS_NETWORK_STATE", "android.permission.WAKE_LOCK", "android.permission.GET_ACCOUNTS", "com.google.android.c2dm.permission.RECEIVE", PackageName + ".permission.C2D_MESSAGE" ); Intent[] intents = new Intent[] { new Intent("com.google.android.c2dm.intent.RECEIVE") .SetPackage(PackageName) .AddCategory(PackageName) }; string receiverPermission = "com.google.android.c2dm.permission.SEND"; bool hasBroadcastReceiverPermission = ManifestInfo.hasReceiverPermission(typeof(ParsePushBroadcastReceiver), receiverPermission, intents); if (hasParsePushService && hasApplicationPermission && hasBroadcastReceiverPermission) { return(true); } // Print errors. Android.Util.Log.Warn(LogTag, "Cannot use GCM for push because AndroidManifest.xml is missing:\n" + (hasParsePushService ? "" : MissingParsePushServiceMessage) + (hasApplicationPermission ? "" : MissingApplicationPermissionMessage) + (hasBroadcastReceiverPermission ? "" : MissingParsePushBroadcastReceiverMessage)); return(false); }