-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[messaging] getToken/deleteToken/onTokenRefresh improvements (#1805)
[IOS] [BUGFIX] [MESSAGING] fix getToken() always returning initialToken (#1510) [ANDROID] [NOTICE] [MESSAGING] deprecate RNFirebaseInstanceIdService in favour of FirebaseMessagingService's onNewToken event. Remove this service from your AndroidManifest.xml [IOS] [ANDROID] [INTERNAL] [MESSAGING] getToken & deleteToken now use Firebase InstanceID token management apis (with an FCM scope).
- Loading branch information
Showing
9 changed files
with
241 additions
and
54 deletions.
There are no files selected for viewing
23 changes: 11 additions & 12 deletions
23
android/src/main/java/io/invertase/firebase/messaging/RNFirebaseInstanceIdService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,24 @@ | ||
package io.invertase.firebase.messaging; | ||
|
||
import android.content.Intent; | ||
import android.support.v4.content.LocalBroadcastManager; | ||
import android.util.Log; | ||
|
||
import com.google.firebase.iid.FirebaseInstanceIdService; | ||
|
||
public class RNFirebaseInstanceIdService extends FirebaseInstanceIdService { | ||
public static final String TOKEN_REFRESH_EVENT = "messaging-token-refresh"; | ||
// public static final String TOKEN_REFRESH_EVENT = "messaging-token-refresh"; | ||
private static final String TAG = "RNFInstanceIdService"; | ||
|
||
// TODO now deprecated, remove in v6 | ||
@Override | ||
public void onTokenRefresh() { | ||
Log.d(TAG, "onTokenRefresh event received"); | ||
|
||
// Build an Intent to pass the token to the RN Application | ||
Intent tokenRefreshEvent = new Intent(TOKEN_REFRESH_EVENT); | ||
|
||
// Broadcast it so it is only available to the RN Application | ||
LocalBroadcastManager | ||
.getInstance(this) | ||
.sendBroadcast(tokenRefreshEvent); | ||
Log.d(TAG, "DEPRECATED onTokenRefresh event received"); | ||
// | ||
// // Build an Intent to pass the token to the RN Application | ||
// Intent tokenRefreshEvent = new Intent(TOKEN_REFRESH_EVENT); | ||
// | ||
// // Broadcast it so it is only available to the RN Application | ||
// LocalBroadcastManager | ||
// .getInstance(this) | ||
// .sendBroadcast(tokenRefreshEvent); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
describe('messaging()', () => { | ||
describe('requestPermission()', () => { | ||
it('returns fcm token', async () => { | ||
if (device.getPlatform() === 'android') { | ||
await firebase.messaging().requestPermission(); | ||
} | ||
}); | ||
}); | ||
|
||
describe('hasPermission()', () => { | ||
it('returns fcm token', async () => { | ||
const bool = await firebase.messaging().hasPermission(); | ||
if (device.getPlatform() === 'android') { | ||
should.equal(bool, true); | ||
} | ||
}); | ||
}); | ||
|
||
describe('RemoteMessage', () => { | ||
it('builds a remote message', async () => { | ||
const message = new firebase.messaging.RemoteMessage(); | ||
message.messageId.should.be.a.String(); // default | ||
message.setMessageId('123'); | ||
message.messageId.should.equal('123'); | ||
|
||
message.setData({ foo: 'bar' }); | ||
message.data.should.be.a.Object(); | ||
|
||
should.equal(message.ttl, 3600); // default | ||
message.setTtl(666); | ||
should.equal(message.ttl, 666); | ||
|
||
should.equal(message.to, undefined); | ||
message.setTo('some-topic'); | ||
|
||
message.build(); | ||
}); | ||
}); | ||
|
||
describe('sendMessage()', () => { | ||
it('sends a message', async () => { | ||
const message = new firebase.messaging.RemoteMessage(); | ||
message.setData({ foo: 'bar' }); | ||
message.setMessageType('data'); | ||
message.setTo( | ||
`${firebase.app().options.messagingSenderId}@gcm.googleapis.com` | ||
); | ||
await firebase.messaging().sendMessage(message); | ||
// TODO test with new firebase admin testing api | ||
// const { promise, resolve, reject } = Promise.defer(); | ||
// const unsubscribe = firebase.messaging().onMessage(msg => { | ||
// resolve(); | ||
// }); | ||
// await promise; | ||
// unsubscribe(); | ||
}); | ||
}); | ||
|
||
describe('subscribeToTopic()', () => { | ||
it('subscribe without error', async () => { | ||
await firebase.messaging().subscribeToTopic('foo-bar-baz'); | ||
// TODO test subscription with new firebase testing api | ||
}); | ||
}); | ||
|
||
describe('unsubscribeFromTopic()', () => { | ||
it('unsubscribe without error', async () => { | ||
await firebase.messaging().unsubscribeFromTopic('foo-bar-baz'); | ||
// TODO test unsub with new firebase testing api | ||
}); | ||
}); | ||
|
||
describe('getToken()', () => { | ||
it('returns fcm token', async () => { | ||
const token = await firebase.messaging().getToken(); | ||
token.should.be.a.String(); | ||
}); | ||
}); | ||
|
||
describe('onTokenRefresh()', () => { | ||
it('triggers when token changes', async () => { | ||
let refreshedToken = null; | ||
let unsubscribe = null; | ||
|
||
const tokenBefore = await firebase.messaging().getToken(); | ||
tokenBefore.should.be.a.String(); | ||
|
||
const { promise, resolve, reject } = Promise.defer(); | ||
unsubscribe = firebase.messaging().onTokenRefresh(newToken => { | ||
unsubscribe(); | ||
|
||
try { | ||
newToken.should.be.a.String(); | ||
tokenBefore.should.not.equal(newToken); | ||
} catch (e) { | ||
return reject(e); | ||
} | ||
|
||
refreshedToken = newToken; | ||
return resolve(); | ||
}); | ||
|
||
await firebase.messaging().deleteToken(); | ||
await sleep(250); | ||
await firebase.iid().delete(); | ||
await sleep(250); | ||
await firebase.iid().get(); | ||
await sleep(250); | ||
|
||
const tokenAfter = await firebase.messaging().getToken(); | ||
tokenAfter.should.be.a.String(); | ||
tokenBefore.should.not.equal(tokenAfter); | ||
|
||
// TODO ios triggers twice, on initial token and new | ||
if (device.getPlatform() === 'android') { | ||
tokenAfter.should.equal(refreshedToken); | ||
} | ||
|
||
await promise; | ||
|
||
await sleep(500); | ||
}); | ||
}); | ||
|
||
describe('deleteToken()', () => { | ||
it('deletes the current fcm token', async () => { | ||
const tokenBefore = await firebase.messaging().getToken(); | ||
tokenBefore.should.be.a.String(); | ||
await firebase.messaging().deleteToken(); | ||
|
||
const tokenAfter = await firebase.messaging().getToken(); | ||
tokenAfter.should.be.a.String(); | ||
tokenBefore.should.not.equal(tokenAfter); | ||
await sleep(500); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require('./app'); |
Oops, something went wrong.