Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes iOS APNS not getting set on repeat boots, adds checkNotificatio… #250

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/android/FirebaseMessagingPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.util.Log;

import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat;

import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.RemoteMessage;
Expand Down Expand Up @@ -48,6 +49,7 @@ public class FirebaseMessagingPlugin extends ReflectiveCordovaPlugin {
private NotificationManager notificationManager;
private FirebaseMessaging firebaseMessaging;
private CallbackContext requestPermissionCallback;
private CallbackContext checkNotificationStatusCallback;

@Override
protected void pluginInitialize() {
Expand Down Expand Up @@ -149,6 +151,35 @@ private void requestPermission(CordovaArgs args, CallbackContext callbackContext
}
}

@CordovaMethod
private void checkNotificationStatus(CallbackContext callbackContext) throws JSONException {
if (Build.VERSION.SDK_INT >= 33) {
Context context = cordova.getActivity().getApplicationContext();
int permissionCheck = ContextCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS);

/* To keep the results the same as iOS, we will use the same values
but we'll only have 2 states: denied and authorized
var _notifStatusEnums = {
0: "notDetermined",
1: "denied",
2: "authorized",
3: "provisional",
4: "ephemeral",
};
*/
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
// The permission is authorized
callbackContext.success(2);
} else {
// The permission is denied
callbackContext.success(1);
}
} else {
// The permission is authorized by default for older android versions
callbackContext.success(2);
}
}

@Override
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) {
for (int result : grantResults) {
Expand Down
1 change: 1 addition & 0 deletions src/ios/FirebaseMessagingPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- (void)onMessage:(CDVInvokedUrlCommand*)command;
- (void)onBackgroundMessage:(CDVInvokedUrlCommand*)command;
- (void)onTokenRefresh:(CDVInvokedUrlCommand*)command;
- (void)checkNotificationStatus:(CDVInvokedUrlCommand*)command;
- (void)sendToken:(NSString*)fcmToken;
- (void)sendNotification:(NSDictionary*)userInfo;
- (void)sendBackgroundNotification:(NSDictionary*)userInfo;
Expand Down
18 changes: 18 additions & 0 deletions src/ios/FirebaseMessagingPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ - (void)pluginInitialize {
if(![FIRApp defaultApp]) {
[FIRApp configure];
}

[[UIApplication sharedApplication] registerForRemoteNotifications];
}

- (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Pass device token to Firebase
[FIRMessaging messaging].APNSToken = deviceToken;
}

- (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"Failed to register for remote notifications: %@", error);
}

- (void)requestPermission:(CDVInvokedUrlCommand *)command {
Expand Down Expand Up @@ -197,4 +208,11 @@ - (void)sendToken:(NSString *)fcmToken {
}
}

- (void)checkNotificationStatus:(CDVInvokedUrlCommand *)command {
UNUserNotificationCenter* notifCenter = [UNUserNotificationCenter currentNotificationCenter];
[notifCenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:(int)settings.authorizationStatus];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
@end
14 changes: 14 additions & 0 deletions www/FirebaseMessaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,17 @@ function(options) {
exec(resolve, reject, PLUGIN_NAME, "requestPermission", [options || {}]);
});
};


exports.checkNotificationStatus =
/**
*
* Checks and returns a status int on wether or not Push notifications are authorized.
* @param {() => void} success Callback function
* @param {(error: string) => void} [error] Error callback function
*/
function () {
return new Promise(function(resolve, reject) {
exec(resolve, reject, PLUGIN_NAME, "checkNotificationStatus", []);
});
};