- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
应用委托(delegate)代码:
import UIKit
import UserNotifications
import Firebase
import FirebaseInstanceID
import FirebaseMessaging
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let gcmMessageIDKey = "gcm.message_id"
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Register for remote notifications. This shows a permission dialog on first run, to
// show the dialog at a more appropriate time move this registration accordingly.
// [START register_for_notifications]
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
// For iOS 10 data message (sent via FCM)
FIRMessaging.messaging().remoteMessageDelegate = self
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
// [END register_for_notifications]
FIRApp.configure()
// [START add_token_refresh_observer]
// Add observer for InstanceID token refresh callback.
NotificationCenter.default.addObserver(self,
selector: #selector(self.tokenRefreshNotification),
name: .firInstanceIDTokenRefresh,
object: nil)
// [END add_token_refresh_observer]
return true
}
/* [START receive_message]*/
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
}
/*[END receive_message]
[START refresh_token] */
func tokenRefreshNotification(_ notification: Notification) {
if let refreshedToken = FIRInstanceID.instanceID().token() {
print("InstanceID token: \(refreshedToken)")
}
// Connect to FCM since connection may have failed when attempted before having a token.
connectToFcm()
}
/* [END refresh_token]
/ [START connect_to_fcm]*/
func connectToFcm() {
// Won't connect since there is no token
guard FIRInstanceID.instanceID().token() != nil else {
return;
}
// Disconnect previous FCM connection if it exists.
FIRMessaging.messaging().disconnect()
FIRMessaging.messaging().connect { (error) in
if error != nil {
print("Unable to connect with FCM. \(error)")
} else {
print("Connected to FCM.")
}
}
}
/* This function is added here only for debugging purposes, and can be removed if swizzling is enabled.
If swizzling is disabled then this function must be implemented so that the APNs token can be paired to
the InstanceID token.*/
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("APNs token retrieved: \(deviceToken)")
// With swizzling disabled you must set the APNs token here.
// FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.sandbox)
if let refreshedToken = FIRInstanceID.instanceID().token() {
print("InstanceID token 1: \(refreshedToken)")
}
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID in didReceiveRemoteNotification: \(messageID)")
}
// Print full message.
print("didReceiveRemoteNotification: \(userInfo)")
completionHandler(UIBackgroundFetchResult.newData)
}
// [END connect_to_fcm]
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Unable to register for remote notifications: \(error.localizedDescription)")
}
/* [START connect_on_active]*/
func applicationDidBecomeActive(_ application: UIApplication) {
connectToFcm()
}
/*[END connect_on_active]
[START disconnect_from_fcm]*/
func applicationDidEnterBackground(_ application: UIApplication) {
//FIRMessaging.messaging().disconnect()
// print("Disconnected from FCM.")
connectToFcm()
print("connected to FCM in Background.")
}
// [END disconnect_from_fcm]
}
extension AppDelegate : UNUserNotificationCenterDelegate {
// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID in UNUserNotificationCenterDelegate: \(messageID)")
}
// Print full message.
print(userInfo)
// Change this to your preferred presentation option
completionHandler([])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID in userNotificationCenter: \(messageID)")
}
// Print full message.
print(userInfo)
completionHandler()
}
}
extension AppDelegate : FIRMessagingDelegate {
// Receive data message on iOS 10 devices while app is in the foreground.
func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
print("applicationReceivedRemoteMessage: \(remoteMessage.appData)")
}
}
最佳答案
要在后台模式下接收和显示通知,只需确保您在“功能”部分启用了“推送通知”。
要在前台模式下显示通知,请在 willPresent 方法的完成 block 中添加警报、角标(Badge)和声音参数。
completionHandler([.alert, .badge, .sound])
// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID in UNUserNotificationCenterDelegate: \(messageID)")
}
// Print full message.
print(userInfo)
// Change this to your preferred presentation option
completionHandler([.alert, .badge, .sound])
}
关于ios - Firebase 通知未在 iOS 应用程序中显示警报或横幅和角标(Badge)应用程序图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42924888/
相信我,我在 Adsense 论坛(西类牙语和英语)中问过这个问题,还检查了以下资源和问题: 1 - 2 - 3 - 4 我有一个实际与 AdMob 配合使用的 AdSense 帐户。我们在 Andr
我想创建一个标题图像/横幅,单击播放图像后,它会垂直扩展以播放YouTube上托管的视频。 此页面准确显示了我要执行的操作: blendedlearningnow.com/about。 我不确定这是否
这里有一个漂亮的 Flash 横幅:http://osc4.template-help.com/drupal_30779/ 。我想知道是否有一个 jquery 库可以让我达到同样的效果。 (点击、滑动
我在一个宽度为 960px 的容器 div 中有一个标题。页眉包含几个完全位于页面中央的 Logo 。我想有一个背景渐变永远延伸到标题后面。出现问题是因为渐变横幅受限于 960px 的宽度。我尝试将横
有谁知道是否可以创建一个功能标题生成器,使用户能够使用颜色、图像和文本对其进行自定义?然后保存并添加到他们的网站? I have created a feature banner demo here
通常在我的网页中,我会有一个 #wrapper DIV 来包装整个页面并设置为如下内容: #wrap {position: relative; width: 1000px; display: bloc
我正在寻找一种动态创建可以放置在其他页面上的横幅的方法。横幅包含一个背景图像、两个重叠图像和两个位于该背景图像之上的文本标签。 是不是只能用绝对定位来实现元素的叠加?我如何确保横幅本身相对放置在集成页
我有一个 900 x 80 的标题区域,以及一个适合该区域左上角的 175 x 75 Logo 。我想要做的是使用 jquery 将 Logo 从标题区域的右侧移动到左侧,然后卡住并设置在左侧。我知道
我成功地在页脚上显示智能横幅,然后将其处理掉。我的问题是显示在应用程序的所有页面上。 我的问题是:如何只显示在一页上? @override void initState() { super.i
我主要是一名开发人员,不知道使用 Adobe Flash CS4。是否有一种简单的方法可以将链接添加到 Flash 横幅。我有 .flv 文件,其中包含库中的一些项目和两层。 请给我一步一步的指示
如何在响应式布局中处理 Adsense 横幅? 据我从 Adsense TOS 中了解到,我几乎不允许在客户端对横幅做任何事情,所以我不能在客户端将横幅切换到较小的分辨率,如果它是低分辨率的. 似乎我
ChannelBannerResource chBannerResource = new ChannelBannerResource(); chBannerResource.setKind("yout
下面附有代码,我正在尝试使用 javascript 制作横幅。 使用 setInterval 我做了一个循环,一个接一个地显示 3 个 div。 我遇到的问题是,当循环结束时,有一个按钮显示“返回”,
我即将在拥有 1000 个活跃用户的应用程序中实现 AdMob 横幅。 我有 1 个 main Activity,其中包含 5 个 fragment,每个 fragment 代表不同的选项卡。 我应该
我尝试在我的应用中实现 AdMob 横幅,但它没有显示。我已经使用了横幅,并且总是使用相同的方式..我有一个带有 View 的 FrameLayout 。此 FrameLayout 位于 Linear
我在我的第一款游戏中使用 Andengine 与 Mobclix 结合使用,每当我尝试运行该应用程序时,我都会在 LogCat 中收到巨大错误。我到处都搜索过,但似乎找不到答案。 FATAL EXCE
JS: document.getElementsByTagName("input, select, textarea, option, optgroup, fieldset, label").onch
我想在页面底部安装一个横幅,但它与主屏幕的内容重叠。在主屏幕中有很多内容并且有滚动。 有人知道为什么吗?谢谢。 XML android:id="
我想在屏幕顶部显示横幅,但是有问题。在我的代码中(这不是我的代码,因为我正在学习过程中)我有两个选择。在屏幕底部的屏幕下方或上方显示横幅。 public void showBanner(fin
我的 AdMob 横幅显示在屏幕顶部而不是底部。 这是我的代码: adView = new AdView(mSingleton); adView.setAdSize(AdSize.SMART_BANN
我是一名优秀的程序员,十分优秀!