我在我的应用中添加了google firebase - 创建谷歌帐户,创建谷歌应用程序,上传APNS认证(.pem和在另一项服务中工作),并从控制台发送推送通知,我的应用程序没有收到它。在Firebase控制台中,我看到状态已完成,但设备的近似数量为" - "
当然,我更新了条款配置文件和APNS证书
service docker stop
rm -r /var/lib/docker/*
service docker start
答案 0 :(得分:11)
我看到的第一件事是没有连接到FIRMessaging
的电话。尝试将其添加到AppDelegate
:
func applicationDidBecomeActive(application: UIApplication) {
FIRMessaging.messaging().connectWithCompletion { error in
print(error)
}
}
答案 1 :(得分:5)
使用以下代码。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
if #available(iOS 10.0, *)
{
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.currentNotificationCenter().delegate = self
UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert]) { (granted, error) in
if granted
{
//self.registerCategory()
}
}
// For iOS 10 data message (sent via FCM)
FIRMessaging.messaging().remoteMessageDelegate = self
}
else
{
let settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: [.Alert,.Badge,.Sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
//Configuring Firebase
FIRApp.configure()
// Add observer for InstanceID token refresh callback.
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.tokenRefreshNotification), name: kFIRInstanceIDTokenRefreshNotification, object: nil)
return true
}
//Receive Remote Notification on Background
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void)
{
FIRMessaging.messaging().appDidReceiveMessage(userInfo)
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)
{
FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Sandbox)
FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Prod)
}
func tokenRefreshNotification(notification: NSNotification)
{
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()
}
func connectToFcm()
{
FIRMessaging.messaging().connectWithCompletion { (error) in
if (error != nil)
{
print("Unable to connect with FCM. \(error)")
}
else
{
print("Connected to FCM.")
}
}
}
func applicationDidBecomeActive(application: UIApplication)
{
connectToFcm()
}
答案 2 :(得分:0)
您可能在PUSH JSON中使用了Data或aps键,该键用于向设备发送通知。它应该可以正常工作,但在极少数情况下,它即使在fcm / send api会给您带来成功的情况下也无法正常工作,但通知不会到达特定的设备。此问题通常发生在iOS设备中。
对我有用的解决方案是添加通知键而不是数据。
<div class="field">
<label class="label">Category</label>
<%= f.collection_select :category_id, Category.all, :id, :name %>
</div>
<div class="field">
<label class="label">Sub Category</label>
<%= f.select :subcategory_id, options_for_select([]) %>
</div>
<script>
$(document).ready(function(){
var getSubcategories = function(category_id){
var subcategories = $('#service_subcategory_id');
$($subcategories).empty();
$.service('/subcategories/find_by_category', { category_id: category_id},
function(data){
$.each(data.subcategories, function(index, subcategory){
var option = $('<option />');
option.attr('value', subcategory.id);
option.text(subcategory.name);
option.appendTo($subcategories);
});
})
};
var getSelectedCategory = function(){
return $('#service_category_id').val();
};
$('#service_category_id').change(function() {
var category_id = getSelectedCategory();
getSubcategories (category_id);
});
getSubcategories(getSelectedCategory());
});
</script>
如果您是从后端发送的,或者您无权访问Firebase控制台。然后,您也可以通过POSTMAN测试并发送通知。但是您必须具有SERVER KEY,它可以从Firebase控制台的Cloud Messaging设置中获取。
readyException.js:6 Uncaught ReferenceError: $subcategories is not defined
at getSubcategories (edit:493)
at HTMLDocument.<anonymous> (edit:520)
at mightThrow (deferred.js:97)
at process (deferred.js:139)
如果您需要了解我们可以在FIREBASE通知中发送的内容,则另外提供详细且完整的有效载荷:
{
"to" : "device token",
"notification":{
"body": "test",
"title": "test"
}
}