当我尝试从火灾基础控制台手动发送推送通知时,推送通知很受欢迎,但是当我尝试从服务器端发送通知时,没有收到任何推送,这里是我的代码 appdelegate
appdelegate.h
//
// AppDelegate.h
// lechef
//
// Created by Zakaria Darwish on 9/5/16.
// Copyright © 2016 CodeBee. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "constant.h"
#include <AudioToolbox/AudioToolbox.h>
#import "MPGNotification.h"
#include <AudioToolbox/AudioToolbox.h>
#include "constant.h"
@import UserNotifications;
@import GoogleMaps;
@import GooglePlaces;
@import Firebase;
@import FirebaseInstanceID;
@import FirebaseMessaging;
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
以及 appdelegate.m
中使用的代码 //
// AppDelegate.m
// lechef
//
// Created by Zakaria Darwish on 9/5/16.
// Copyright © 2016 CodeBee. All rights reserved.
//
#import "AppDelegate.h"
@import UserNotifications;
@import GoogleMaps;
@import GooglePlaces;
@import Firebase;
@import FirebaseInstanceID;
@import FirebaseMessaging;
@interface AppDelegate ()
{
MPGNotification *notification;
}
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} else {
// iOS 10 or later
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
UNAuthorizationOptions authOptions =
UNAuthorizationOptionAlert
| UNAuthorizationOptionSound
| UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter]
requestAuthorizationWithOptions:authOptions
completionHandler:^(BOOL granted, NSError * _Nullable error) {
}
];
// For iOS 10 display notification (sent via APNS)
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
// For iOS 10 data message (sent via FCM)
[[FIRMessaging messaging] setRemoteMessageDelegate:self];
#endif
}
[[UIApplication sharedApplication] registerForRemoteNotifications];
[UINavigationBar appearance].backgroundColor = [UIColor colorWithRed:blackRedColor green:blackGreenColor blue:blackBlueColor alpha:ALFA];
self.window.tintColor = [UIColor colorWithRed:greenRedColor green:greenGreenColor blue:greenBlueColor alpha:ALFA];
[GMSServices provideAPIKey:@"******************************"];
float ver = [[[UIDevice currentDevice] systemVersion] floatValue];
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:blackRedColor green:blackGreenColor blue:blackBlueColor alpha:ALFA]];
if(ver >= 8 && ver<=9)
{
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
{
[[UIApplication sharedApplication] registerForRemoteNotifications];
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
}else if (ver >=9){
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else{
//iOS6 and iOS7 specific code
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert];
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshNotification:)
name:kFIRInstanceIDTokenRefreshNotification object:nil];
[FIRApp configure];
[self connectToFcm];
[[UIApplication sharedApplication] registerForRemoteNotifications];
[[FIRMessaging messaging] subscribeToTopic:@"/topics/news"];
return YES;
}
// Receive displayed notifications for iOS 10 devices.
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
// Print message ID.
NSDictionary *userInfo = notification.request.content.userInfo;
NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]);
// Print full message.
NSLog(@"%@", userInfo);
}
// Receive data message on iOS 10 devices.
- (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage {
// Print full message
NSLog(@"%@", [remoteMessage appData]);
}
#endif
- (void)tokenRefreshNotification:(NSNotification *)notification {
// Note that this callback will be fired everytime a new token is generated, including the first
// time. So if you need to retrieve the token as soon as it is available this is where that
// should be done.
NSString *refreshedToken = [[FIRInstanceID instanceID] token];
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isTokenInserted"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"InstanceID token: %@", refreshedToken);
}
-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[[FIRMessaging messaging] subscribeToTopic:@"/topics/news"];
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// NSDictionary *newUserInfo = [userInfo objectForKey:@"notification"];
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateBackground || state == UIApplicationStateInactive)
{
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.userInfo = userInfo;
localNotification.alertTitle = [userInfo objectForKey:@"title"];
localNotification.alertBody = [userInfo objectForKey:@"body"];
localNotification.applicationIconBadgeNumber = 0;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.fireDate = [NSDate date];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
else if (state == UIApplicationStateActive) {
AudioServicesPlaySystemSound(1054);
UIImage *icon = [UIImage imageNamed:@"placeholderimage"];
NSString *title = [userInfo objectForKey:@"title"];
NSString *body = [userInfo objectForKey:@"body"];
notification = [MPGNotification notificationWithTitle:title subtitle:body backgroundColor:[UIColor colorWithRed:blackRedColor green:greenGreenColor blue:greenBlueColor alpha:ALFA] iconImage:icon];
notification.duration = 5.0;
notification.swipeToDismissEnabled = NO;
[notification show];
}
}
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]];
token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"content---%@", token);
}
- (void)connectToFcm {
[[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Unable to connect to FCM. %@", error);
} else {
NSLog(@"Connected to FCM.");
}
}];
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
这是我正在使用的PHP脚本: PHP脚本
<php?
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=***************************
{
"to": "/topics/news",
"data": {
"message": "This is a Firebase Cloud Messaging Topic Message!",
}
}
?>