aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firebase/Messaging/FIRMessagingReceiver.m
blob: 981dfb131727f6c251ff917ed437c1dd6355bb38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * Copyright 2017 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import "FIRMessagingReceiver.h"

#import <UIKit/UIKit.h>

#import "FIRMessaging.h"
#import "FIRMessaging_Private.h"
#import "FIRMessagingLogger.h"

static NSString *const kUpstreamMessageIDUserInfoKey = @"messageID";
static NSString *const kUpstreamErrorUserInfoKey = @"error";

// Copied from Apple's header in case it is missing in some cases.
#ifndef NSFoundationVersionNumber_iOS_9_x_Max
#define NSFoundationVersionNumber_iOS_9_x_Max 1299
#endif

static int downstreamMessageID = 0;

@implementation FIRMessagingReceiver

#pragma mark - FIRMessagingDataMessageManager protocol

- (void)didReceiveMessage:(NSDictionary *)message withIdentifier:(nullable NSString *)messageID {
  if (![messageID length]) {
    messageID = [[self class] nextMessageID];
  }

  if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_9_x_Max) {
    // Use delegate method for iOS 10
    [self scheduleIos10NotificationForMessage:message withIdentifier:messageID];
  } else {
    // Post notification directly to AppDelegate handlers. This is valid pre-iOS 10.
    [self scheduleNotificationForMessage:message];
  }
}

- (void)willSendDataMessageWithID:(NSString *)messageID error:(NSError *)error {
  NSNotification *notification;
  if (error) {
    NSDictionary *userInfo = @{
      kUpstreamMessageIDUserInfoKey : [messageID copy],
      kUpstreamErrorUserInfoKey : error
    };
    notification = [NSNotification notificationWithName:FIRMessagingSendErrorNotification
                                                 object:nil
                                               userInfo:userInfo];
    [[NSNotificationQueue defaultQueue] enqueueNotification:notification postingStyle:NSPostASAP];
    FIRMessagingLoggerDebug(kFIRMessagingMessageCodeReceiver000,
                            @"Fail to send upstream message: %@ error: %@", messageID, error);
  } else {
    FIRMessagingLoggerDebug(kFIRMessagingMessageCodeReceiver001, @"Will send upstream message: %@",
                            messageID);
  }
}

- (void)didSendDataMessageWithID:(NSString *)messageID {
  // invoke the callbacks asynchronously
  FIRMessagingLoggerDebug(kFIRMessagingMessageCodeReceiver002, @"Did send upstream message: %@",
                          messageID);
  NSNotification * notification =
      [NSNotification notificationWithName:FIRMessagingSendSuccessNotification
                                    object:nil
                                  userInfo:@{ kUpstreamMessageIDUserInfoKey : [messageID copy] }];

  [[NSNotificationQueue defaultQueue] enqueueNotification:notification postingStyle:NSPostASAP];
}

- (void)didDeleteMessagesOnServer {
  FIRMessagingLoggerDebug(kFIRMessagingMessageCodeReceiver003,
                          @"Will send deleted messages notification");
  NSNotification * notification =
      [NSNotification notificationWithName:FIRMessagingMessagesDeletedNotification
                                    object:nil];

  [[NSNotificationQueue defaultQueue] enqueueNotification:notification postingStyle:NSPostASAP];
}

#pragma mark - Private Helpers
// As the new UserNotifications framework in iOS 10 doesn't support constructor/mutation for
// UNNotification object, FCM can't inject the message to the app with UserNotifications framework.
// Define our own protocol, which means app developers need to implement two interfaces to receive
// display notifications and data messages respectively for devices running iOS 10 or above. Devices
// running iOS 9 or below are not affected.
- (void)scheduleIos10NotificationForMessage:(NSDictionary *)message
                             withIdentifier:(NSString *)messageID {
  FIRMessagingRemoteMessage *wrappedMessage = [[FIRMessagingRemoteMessage alloc] init];
  // TODO: wrap title, body, badge and other fields
  wrappedMessage.appData = [message copy];
  [self.delegate receiver:self receivedRemoteMessage:wrappedMessage];
}

- (void)scheduleNotificationForMessage:(NSDictionary *)message {
  SEL newNotificationSelector =
      @selector(application:didReceiveRemoteNotification:fetchCompletionHandler:);
  SEL oldNotificationSelector = @selector(application:didReceiveRemoteNotification:);

  dispatch_async(dispatch_get_main_queue(), ^{
    id<UIApplicationDelegate> appDelegate = [[UIApplication sharedApplication] delegate];
    if ([appDelegate respondsToSelector:newNotificationSelector]) {
      // Try the new remote notification callback
      [appDelegate application:[UIApplication sharedApplication]
  didReceiveRemoteNotification:message
        fetchCompletionHandler:^(UIBackgroundFetchResult result) {}];

    } else if ([appDelegate respondsToSelector:oldNotificationSelector]) {
      // Try the old remote notification callback
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
      [appDelegate application:
       [UIApplication sharedApplication] didReceiveRemoteNotification:message];
#pragma clang diagnostic pop
    } else {
      FIRMessagingLoggerError(kFIRMessagingMessageCodeReceiver005,
                              @"None of the remote notification callbacks implemented by "
                              @"UIApplicationDelegate");
    }
  });
}

+ (NSString *)nextMessageID {
  @synchronized (self) {
    ++downstreamMessageID;
    return [NSString stringWithFormat:@"gcm-%d", downstreamMessageID];
  }
}

@end