نکته در مورد پکیج ios
پشتیبانی از پکیج iOS در حال حاضر متوقف میباشد و بروزرسانی انجام نمیگیرد
نصب و راهاندازی پوشه در Xcode
پیشنیازها
- برای داشتن امکان دریافت
Push notificationدر برنامه خود، بایدApple Develeper Accountداشته باشید. - با استفاده از
Apple Developer Accountخود، یک کلیدAPNsایجاد کنید. - در کنسول فایربیس یک پروژه ایجاد کنید و
Server keyوSender IDدریافت کنید. - در کنسول فایربیس، یک برنامه ایجاد کرده و فایل
GoogleService-Info.plistدریافتی را به پروژه خود درXcodeاضافه کنید. - کلید
APNsخود را در برنامهتان در فایربیس آپلود کنید. - اپلیکشین خود را در کنسول پوشه ثبت کنید و
appIdمتناظر با برنامه خود را دریافت کنید. - دستگاه تست باید یک دستگاه فیزیکی باشد. (در حال حاضر
simulatorهایXcodeازPush Notificationپشتیبانی نمیکنند.) - قابلیتهای موردنیاز را در
Xcodeبه برنامه خود اضافه کنید. - برای دریافت عکس، فیلم و ... در
Pushe Notification، یکNotification Service Extensionبه برنامه خود اضافه کنید. - برخی از امکانات پوشه مانند آمار Delivery وابسته به اضافه کردن
App-Groupsهستند. بنابراین در صورتی که این آمار و امکانات برای شما مهم هستند، بایدApp-Groupsرا به اپلیکیشن خود اضافه کنید.
نصب پوشه با استفاده از cocoapods
در Podfile پوشه را برای Target اصلی و Notification Service Extension اضافه کنید:
target 'PusheDemo' do
use_frameworks!
pod 'Pushe', '1.0.16'
target 'PusheDemoTests' do
inherit! :search_paths
# Pods for testing
end
target 'PusheDemoUITests' do
inherit! :search_paths
# Pods for testing
end
end
target 'DemoNotificationServiceExtension' do
use_frameworks!
pod 'Pushe', '1.0.16'
end
سپس با دستور زیر میتوانید پوشه را نصب کنید.
pod install --repo-update
اضافه کردن کلیدهای موردنیاز
در فایل Info.plist متناظر با Target اصلی برنامه، کلیدها و مقادیر زیر را وارد کنید.
<plist version="1.0">
<dict>
...
<key>FirebaseAppDelegateProxyEnabled</key>
<false/>
<key>Pushe</key>
<dict>
<key>AppGroupsId</key>
<string>Enter your App-Groups id</string>
<key>ManifestKey</key>
<string>Enter your Menifest-Key from console.pushe.co</string>
</dict>
</dict>
</plist>
برای نمونه:
<plist version="1.0">
<dict>
<key>FirebaseAppDelegateProxyEnabled</key>
<false/>
<key>Pushe</key>
<dict>
<key>AppGroupsId</key>
<string>group.my.uri.myApp.shared</string>
<key>ManifestKey</key>
<string>cWQyMzR3bTlsOxBUcjQ0ZUBJaxZAOTA4NDU35Tk1MDcy</string>
</dict>
</dict>
</plist>
همچنین در فایل Info.plist متناظر با Notification Service Extension، کلیدها و مقادیر زیر را وارد کنید.
<plist version="1.0">
<dict>
<key>Pushe</key>
<dict>
<key>AppGroupsId</key>
<string>Enter your App-Groups id</string>
</dict>
</dict>
</plist>
- برای راهنمایی در مورد نحوه ایجاد
App-Groupsبه لینک زیر مراجعه کنید.
اضافهکردن کدهای موردنیاز
و در Target مربوط به NotificationServiceExtension:
- Swift
- Objective-C
// NotificationService.swift file
import UserNotifications
import Pushe
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
self.bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = self.bestAttemptContent {
PusheClient.shared.didReceiveNotificationExtensionRequest(mutableContent: bestAttemptContent, contentHandler: contentHandler)
}
}
override func serviceExtensionTimeWillExpire() {
if let contentHandler = self.contentHandler, let bestAttemptContent = self.bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
// NotificationService.m file
#import "NotificationService.h"
@import Pushe;
@interface NotificationService ()
@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;
@end
@implementation NotificationService
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];
[PusheClient.shared didReceiveNotificationExtensionRequest:self.bestAttemptContent :self.contentHandler];
}
- (void)serviceExtensionTimeWillExpire {
self.contentHandler(self.bestAttemptContent);
}
@end
فراخوانی کدهای موردنیاز
در کلاس AppDelegate، Pushe را import کنید. سپس تابع initialize را فراخوانی کنید.
- Swift
- Objective-C
// AppDelegate.swift file
import UIKit
import Pushe
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// initializing Pushe
PusheClient.shared.initialize()
// Override point for customization after application launch.
return true
}
}
// AppDelegate.m file
#import "AppDelegate.h"
@import Pushe;
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// initializing Pushe
[PusheClient.shared initialize];
// Override point for customization after application launch.
return YES;
}
@end
تست و ثبت دستگاه در پوشه
پس از اجرای برنامه و فراخوانی کد رجیستر پوشه، باید در کنسول Xcode لاگهای زیر را ببینید (ممکن است به دلیل ارتباط با سرور این پروسه چند ثانیه طول بکشد):
Setting up Pushe ...
apns-token:<APNS_TOKEN>
fcm-token:<FCM_TOKEN>
registering in Pushe ...
📗 -> successfully registered in Pushe
ادامهی کار
(بر روی لینک مورد نظر کلیک کنید)
پروژهی نمونه در گیتهاب
بررسی امکانات در نمونهای از قبل طراحی شده دارای تمام امکانات کتابخانهی پوشه
سوالات و مشکلات احتمالی
در صورتی که در یکی از مراحل زیر به مشکلی برخوردید یا هر سوالی در مورد کتابخانه دارید