I have developed an iOS App using Xamarin and integrated FCM (Firebase Cloud Messaging) for push notifications. Its working fine on development phase, but on beta testing through test flight, the FCM token automatically regenerates or refreshes after some time (in between 5-10 minutes).
void TokenRefreshNotification(object sender, NSNotificationEventArgs e)
{
// This method 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.
//var refreshedToken = InstanceId.SharedInstance.Token;
var token = InstanceId.SharedInstance.Token;
WriteLog("Token Refresh");
ConnectToFCM();
// TODO: If necessary send token to application server.
}
public static void ConnectToFCM()
{
Messaging.SharedInstance.Connect(error =>
{
if (InstanceId.SharedInstance.Token != null)
{
var token = InstanceId.SharedInstance.Token;
// FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Unknown)
// ApnsTokenType = ApnsTokenType.Unknown;
if (!Settings.DeviceId.Equals(token))
{
Settings.DeviceId = token;
Console.WriteLine("Token Updated");
}
}
Console.WriteLine($"Token: {InstanceId.SharedInstance.Token}");
});
}
Solved.
My Observation is from same API 2 different App is installed that's causing the problem. When i uninstalled one App(which is previous one only bundle ID is different) now its working fine. No token refreshed automatically after few minutes.
Related
I make app by React Native.
Few days ago,I was able to receive FCM. But now I cannot.
Client app permissions notification, I set Xcode(capability),firebase and Apple Developer(APNs setting).
And console.log tells me success.
But I cannot receive notification. Although few days ago I could.
Is this error in firebase??
I don't know cause, please help me m(_ _)m
=======environment==========
Xcode Version 11.3
"react-native-firebase": "^5.6.0"
"react-native": "0.61.5"
"firebase-admin": "^8.9.0"
=======addition========
Regenerating APNs and setting, I could receive notification.
But next day, I couldn't receive notification.
APNs is valid in only one day???
Most probably, in your Firebase console number of potential users are eligible for this campaign is 0 currently.
"Estimate based on approximately 0 users who are registered to receive notifications. Some targeted users will not see the message due to device inactivity. For recurring campaigns, estimate is for initial send only."
Possible solution:
1) If that's the case (0 users are registered)
--> Check if the fcmToken is received.
getFcmToken = async () => {
const fcmToken = await firebase.messaging().getToken();
if (fcmToken) {
console.log(fcmToken);
this.showAlert(‘Your Firebase Token is:’, fcmToken);
} else {
this.showAlert(‘Failed’, ‘No token received’);
}
}
2) If the number is not zero
--> Most probably your app is opened in foreground and the notification is not displayed.
--> Try to lock your iPhone screen and the notification will appear, else try to handle it which the app is in foreground
messageListener = async () => {
this.notificationListener = firebase.notifications().onNotification((notification) => {
const { title, body } = notification;
this.showAlert(title, body);
});
this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
const { title, body } = notificationOpen.notification;
this.showAlert(title, body);
});
const notificationOpen = await firebase.notifications().getInitialNotification();
if (notificationOpen) {
const { title, body } = notificationOpen.notification;
this.showAlert(title, body);
}
this.messageListener = firebase.messaging().onMessage((message) => {
console.log(JSON.stringify(message));
});
}
3) Check if the cert has expired (unlikely) since you mentioned that it's still working a few days back.
I am working on an ionic app and I have integrated firebase push notifications. On android everything works fine, but on iOS I'm getting below error in the log. I have enabled push notifications capabilities on XCode as well. Any idea why am I getting this error?
FIRMessaging received data-message, but FIRMessagingDelegate's-messaging:didReceiveMessage: not implemented
I got it fixed.
I am using below method to get the token.
// Get permission from the user
async getToken() {
return await new Promise(async (resolve) => {
let token: String;
if (this.platform.is('android')) {
token = await this.firebaseNative.getToken();
console.log('Android device instance id : ', token);
resolve(token);
}
if (this.platform.is('ios')) {
token = await this.firebaseNative.getToken();
await this.firebaseNative.grantPermission();
console.log('iOS device instance id : ', token);
resolve(token);
}
});
}
the issue was with this code line.
await this.firebaseNative.grantPermission();
once it's removed it worked.
I am trying to get push notifications to work on iOS, but I can not get access to the device token!
My Unity version is 5.4.1f1.
I have enabled the Push Notifications capability in XCode and all the certificates are setup correctly:
In my script in the start method I call this:
UnityEngine.iOS.NotificationServices.RegisterForNotifications
(UnityEngine.iOS.NotificationType.Alert | UnityEngine.iOS.NotificationType.Badge
| UnityEngine.iOS.NotificationType.Sound, true);
Then from the update method I call this method:
private bool RegisterTokenWithPlayfab( System.Action successCallback,
System.Action<PlayFabError> errorCallback )
{
byte[] token = UnityEngine.iOS.NotificationServices.deviceToken;
if(token != null)
{
// Registration on backend
}
else
{
string errorDescription = UnityEngine.iOS.NotificationServices.registrationError;
Debug.Log( "Push Notifications Registration failed with: " + errorDescription );
return false;
}
}
The token keeps being empty, so the else-branch is entered every call. Also, the registrationError keeps being empty.
Can someone point me in the right direction on this? What else can I try or how can I get more infos on what is going wrong??
Try this one
Go to your application target. Choose Capabilities and ensure that ‘Push Notifications’ is enabled there.
You need to check deviceToken in a Coroutine or Update.
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using NotificationServices = UnityEngine.iOS.NotificationServices;
using NotificationType = UnityEngine.iOS.NotificationType;
public class NotificationRegistrationExample : MonoBehaviour
{
bool tokenSent;
void Start()
{
tokenSent = false;
NotificationServices.RegisterForNotifications(
NotificationType.Alert |
NotificationType.Badge |
NotificationType.Sound, true);
}
void Update()
{
if (!tokenSent)
{
byte[] token = NotificationServices.deviceToken;
if (token != null)
{
// send token to a provider
string token = System.BitConverter.ToString(token).Replace('-', '%');
Debug.Log(token)
tokenSent = true;
}
}
}
}
The implementation is horrible, but we don't have callbacks from Unity side so we need to keep listening that variable value.
Check the documentation:
https://docs.unity3d.com/ScriptReference/iOS.NotificationServices.RegisterForNotifications.html
Also seems to need internet. I guess there is an Apple service going on there.
Yes, registration error is empty even when user deniy permissions.
What i did is to use UniRX and set an Observable fron a Coroutine with a time out so it dont keep forever asking for it.
If you accepted and you do not received the token might be the internet conection. And i guess you are teying this on a real device.
I am trying for a while now to implement this flow: When user adds some files on server app, notification should trigger and send from server to FCM and that from there to pass message saying something like: 'New file has been added'.
Basically I want to inform mobile device user that something on server has been changed.
I have tried many things, but nothing seems to work as I would expect, at least.
On the mobile side I have set up Firebase inside my Xamarin.Android project, and when I am sending notifications directly from Firebase console, I get notifications, and everything is good.
But I don't want to send notifications via Firebase console, I would rather send notification from server (which is ASP.NET MVC project) to Firebase console and then pass it from there to android device.
My first question would be: Has anybody got an idea how can I inform web app about device_id? Is there some way that android device send this information on server? And maybe from there I can store that data and update it occasionally, since it is basically a refresh token.
My second problem is this: Even when I hard code current device_id of an active android device and try to send a message from server whit this code:
public class FirebaseService : IFirebaseService
{
public void SendMessageToClientApplication(string message, string serverApiKey, string senderId, string deviceId)
{
AndroidFCMPushNotificationStatus result = new AndroidFCMPushNotificationStatus();
try
{
result.Successful = false;
result.Error = null;
deviceId = "eMk6mD8P8Dc:APA91bG5Lmqn4Hwb4RZJ1Mkdl8Rf_uYQsQCEfDJK334tzSvIGzdao7o2X6VmtcTEp_Li0mG8iUoUT7-_RnZxQKocHosZwx6ITWdpmQyCwUv60IIIy0vxNlEaccT6RqK6c-cE1C6I3FTT";
var value = message;
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
tRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
tRequest.Headers.Add(string.Format("Authorization: key={0}", serverApiKey));
tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message="
+ value + "&data.time=" + DateTime.Now.ToString() + "®istration_id=" + deviceId + "";
Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
tRequest.ContentLength = byteArray.Length;
using (Stream dataStream = tRequest.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
using (WebResponse tResponse = tRequest.GetResponse())
{
using (Stream dataStreamResponse = tResponse.GetResponseStream())
{
using (StreamReader tReader = new StreamReader(dataStreamResponse))
{
String sResponseFromServer = tReader.ReadToEnd();
result.Response = sResponseFromServer;
}
}
}
}
}
catch (Exception ex)
{
result.Successful = false;
result.Response = null;
result.Error = ex;
}
}
}
I get nothing both in Firebase console and of course nothing on device as well.
I have tried to implement Firebase web as javascript on my server app like this:
<script>
var config = {
apiKey: "mykey",
authDomain: "myauthdomain",
databaseURL: "mydatabaseurl",
projectId: "myprojectid",
storageBucket: "mystoragebucket",
messagingSenderId: "mysenderid"
};
window.onload = function () {
firebase.initializeApp(config);
const messaging = firebase.messaging();
messaging.requestPermission()
.then(function () {
console.log('Notification permission granted.');
return messaging.getToken()
})
.then(function (token) {
console.log(token);
})
.catch(function (err) {
console.log('Unable to get permission to notify.', err);
});
messaging.onMessage(function (payload) {
console.log('onMessage: ', payload);
});
}
</script>
But this code gets some kind of a different device_id(aka token), probably one generated for that server machine.
Does anybody has experience with sending device_id to server app and from there sending notification message to Firebase console? I would appreciate some code examples, tutorials or anything that can help, since I was unable to find something useful during my google search.
My first question would be: Has anybody got an idea how can I inform web app about device_id?
The most common approach is to store the list of device tokens (each device that uses FCM has such a token) in a database, such as the Firebase Database. There is an example of this in the Cloud Functions for Firebase documentation. In this example the devices receiving the messages are web pages, but the approach is the same for iOS and Android.
I also recommend reading Sending notifications between Android devices with Firebase Database and Cloud Messaging. In this article, instead of sending to a device token, each user subscribes to a topic. That prevents having to manage the device tokens in your code.
I'm trying to use Xamarin native iOS library to authenticate with Facebook and access Graph API.
According to release 4.0.1.1 notes for the component (I didn't find any other documentation anywhere)
FBSDKTokenCachingStrategy. No alternative. LoginManager class caches
tokens to keychain automatically. You can observe token changes to do
manual post processing.
However this doesn't seem to be happening. When my iOS application starts I create LoginManager instance and call Init. However after that AccessToken.CurrentAccessToken is still null. It is only populated with data after I call LogInWithReadPermissionsAsync on the LoginManager.
Am I missing something or is it a bug.
Here's my code.
public bool IsLoggedIn
{
get
{
return AccessToken.CurrentAccessToken != null &&
AccessToken.CurrentAccessToken.ExpirationDate.ToDateTime() > DateTime.Now;
}
}
public Task<AccessToken> FacebookLoginInternal()
{
lock (monitor)
{
if (_loginTask == null)
{
LoginManager manager = new LoginManager();
manager.Init();
if (IsLoggedIn)
{
var ts = new TaskCompletionSource<AccessToken>();
ts.SetResult(AccessToken.CurrentAccessToken);
_loginTask = ts.Task;
}
else
{
var loginResult = manager.LogInWithReadPermissionsAsync(
new string[] { "email", "user_friends" });
_loginTask = loginResult.ContinueWith(r =>
{
return r.Result.Token;
});
}
}
return _loginTask;
}
As per response from Xamarin support (thank you!)
The following code fixes the issue:
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
return ApplicationDelegate.SharedInstance.FinishedLaunching(app, options);
}
This would seem to be expected behavior? You won't have a token until you log in, and this seems expected. I believe you may have misunderstood the note you pasted. It does not say that the token is cached as soon as you instantiate LoginManager or call Init on it, just that LoginManager will cache the token. It can't cache a token until a token is generated when you log in. That is why (I believe) Guilherme Torres Castro asked if the token is the same after a second call to LogInWithReadPermissionsAsync. If so, then the token was cached upon login.
Update: Communication with the OP via other channels indicates that I misunderstood. Log in is not maintained after app termination and relaunch, whereas in the native Obj-C Facebook iOS SDK it is. A bug has been filed: https://bugzilla.xamarin.com/show_bug.cgi?id=30287