I have made the switch from GCM to FCM in my android app. But I have not touched the server portion of the code since supposedly I don’t have to. But now, when server sends a notification, my device is no longer seeing it. So did I misunderstand the following?
https://developers.google.com/cloud-messaging/android/android-migrate-fcm#update_server_endpoints
Updating these endpoints is not strictly required, as Google will
continue to support the existing GCM endpoints.
MANIFEST
<!-- for notifications -->
<!--<uses-permission android:name="android.permission.WAKE_LOCK"/>-->
<!--<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>-->
<!--<uses-permission android:name="com.business.myapp.android.permission.C2D_MESSAGE"/>-->
…
<application …>
…
<!-- START GCM SECTION -->
<!--<receiver-->
<!--android:name="com.google.android.gms.gcm.GcmReceiver"-->
<!--android:exported="true"-->
<!--android:permission="com.google.android.c2dm.permission.SEND">-->
<!--<intent-filter>-->
<!--<action android:name="com.google.android.c2dm.intent.RECEIVE"/>-->
<!--<category android:name="com.business.myapp"/>-->
<!--</intent-filter>-->
<!--</receiver>-->
<service
android:name=".data.service.notification.BusinessGcmListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".data.service.notification.BusinessInstanceIDListenerService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service
android:name=".data.service.notification.RegistrationIntentService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
</intent-filter>
</service>
<!-- END GCM SECTION -->
Note that when I send notification from the Firebase Console it comes through: if the app is in the foreground, it goes through onMessageReceived; otherwise, I just get a notification in status bar. Basically I follow the instructions on the link provided.
Here is onTokenRefresh
#Override
public void onTokenRefresh() {
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);//intent service to send token to my server
}
Do you change your json-service.json of on root /app of on application ?
Related
I am following this documentation for receiving push notification on react native android https://github.com/evollu/react-native-fcm, I am able to receive the device token id, but I am not able to receive any notification when I am using sending manually from firebase using that device id:
This is the code on my react native app:
import {Platform} from 'react-native';
import FCM, {FCMEvent, RemoteNotificationResult, WillPresentNotificationResult, NotificationType} from 'react-native-fcm';
// this shall be called regardless of app state: running, background or not running. Won't be called when app is killed by user in iOS
FCM.on(FCMEvent.Notification, async (notif) => {
// there are two parts of notif. notif.notification contains the notification payload, notif.data contains data payload
if(notif.local_notification){
//this is a local notification
}
if(notif.opened_from_tray){
//iOS: app is open/resumed because user clicked banner
//Android: app is open/resumed because user clicked banner or tapped app icon
}
// await someAsyncCall();
if(Platform.OS ==='ios'){
//optional
//iOS requires developers to call completionHandler to end notification process. If you do not call it your background remote notifications could be throttled, to read more about it see https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623013-application.
//This library handles it for you automatically with default behavior (for remote notification, finish with NoData; for WillPresent, finish depend on "show_in_foreground"). However if you want to return different result, follow the following code to override
//notif._notificationType is available for iOS platfrom
switch(notif._notificationType){
case NotificationType.Remote:
notif.finish(RemoteNotificationResult.NewData) //other types available: RemoteNotificationResult.NewData, RemoteNotificationResult.ResultFailed
break;
case NotificationType.NotificationResponse:
notif.finish();
break;
case NotificationType.WillPresent:
notif.finish(WillPresentNotificationResult.All) //other types available: WillPresentNotificationResult.None
break;
}
}
});
FCM.on(FCMEvent.RefreshToken, (token) => {
console.log(token)
// fcm token may not be available on first load, catch it here
});
class App extends Component {
componentDidMount() {
// iOS: show permission prompt for the first call. later just check permission in user settings
// Android: check permission in user settings
FCM.requestPermissions().then(()=>console.log('granted')).catch(()=>console.log('notification permission rejected'));
FCM.getFCMToken().then(token => {
console.log(token)
// store fcm token in your server
});
this.notificationListener = FCM.on(FCMEvent.Notification, async (notif) => {
// optional, do some component related stuff
});
// initial notification contains the notification that launchs the app. If user launchs app by clicking banner, the banner notification info will be here rather than through FCM.on event
// sometimes Android kills activity when app goes to background, and when resume it broadcasts notification before JS is run. You can use FCM.getInitialNotification() to capture those missed events.
// initial notification will be triggered all the time even when open app by icon so send some action identifier when you send notification
FCM.getInitialNotification().then(notif => {
console.log(notif)
});
}
componentWillUnmount() {
// stop listening for events
this.notificationListener.remove();
}
Android Manifest file looks as below:
<?xml version="1.0" encoding="utf-8"?>
<manifest package="fi.rogerstudio.possis"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/>
<uses-permission android:name="com.example.healthgps.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-feature android:name="android.hardware.location.gps" />
<application
android:name=".MainApplication"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true">
<activity android:name=".MainActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:theme="#style/Theme.Exponent.Light"
android:windowSoftInputMode="adjustResize"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<data android:scheme="exp6a747c4f7f604b89a93d9f3d281cbb77"/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
</activity>
<service
android:name="io.invertase.firebase.messaging.MessagingService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name="io.invertase.firebase.messaging.InstanceIdService" android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service android:name="com.evollu.react.fcm.MessagingService" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service android:name="com.evollu.react.fcm.InstanceIdService" android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<!-- The Facebook SDK runs FacebookInitProvider on startup and crashes if there isn't an ID here -->
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="fb0"/>
<!-- react-native-background-geolocation licence -->
<meta-data android:name="com.transistorsoft.locationmanager.license" android:value="14ef777231a8412c8b2d109443999356a2c13e62c07e2868345f5e13f01b3c83" />
<meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="#mipmap/ic_launcher"/>
<receiver android:name="com.evollu.react.fcm.FIRLocalMessagingPublisher"/>
<receiver android:enabled="true" android:exported="true" android:name="com.evollu.react.fcm.FIRSystemBootEventReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
I see your current problem is about your installed package. You made some conflict package install.
Please remove io.invertase.firebase.messaging from your app.
npm uninstall react-native-firebase --save
Then you also need to remove from your settings.gradle
I'm using Firebase for receiving push notifications. My problem is that i want to make an option for users available, where they can disable these Notifications.
<application android:label="MyApp" android:theme="#android:style/Theme.NoTitleBar" android:icon="#drawable/ico_myapp_application">
<receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" />
<receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
<!-- END Manual Firebase Additions -->
</application>
Is there a way to prevent the app from register/creating the FirebaseMessagingService?
You could make options for users to Unsubscribe from your Firebase topic, they would stop getting the notifications. Unless if you are sending it directly to their ID.
FirebaseMessaging.getInstance().subscribeToTopic("news");
and then unsubscribe users
FirebaseMessaging.getInstance().unsubscribeFromTopic("news");
I am using mixpanel in my app and I am getting 2 notifications from mixpanel. I am already using gcm on my device for chat. How can I receive both mixpanel and my notifications. BTW I generate GCM reg_id via code.
Manifest file:
<receiver
android:name=".gcm.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter android:priority="100">
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name=“com.example.Mainactivity.gcm" />
</intent-filter>
</receiver>
<receiver android:name="com.mixpanel.android.mpmetrics.GCMReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.example.Mainactivity.gcm" />
</intent-filter>
</receiver>
<service android:name=".gcm.GcmIntentService" />
I have added :
mixpanel.registerSuperProperties(props);
mixpanel.identify(id);
mixpanel.getPeople().identify(id);
mixpanel.getPeople().setPushRegistrationId(reg_id);
mixpanel.getPeople().initPushHandling(SENDER_ID);
in my GcmBroadcastReceiver I have added:
if (intent.getExtras().containsKey("mp_message")) {
String mp_message=intent.getExtras().getString("mp_message");
}
Check if you have added same device token in multiple people's property (comma separated)
As you are saying you implemented GCM before and you integrated Mixpanel afterwards. In that case you should remove the Mixpanel GCMReceiver tag as mentioned in the official documentation:
If you are handling registration and receiving notifications yourself,
you shouldn't include the Mixpanel GCMReceiver tag in your
AndroidManifest.xml file.
https://mixpanel.com/help/reference/android-push-notifications
Like some others, I too can't seem to get GCM to work. In particular, I cannot see why execution is not reaching GcmListenerService.onMessageReceived().
I have followed the official docs and have managed to (I think) do the following correctly:
get a registration token using InstanceID
send this token back to my local Rails server for storage
subsequently, use this same token to send a message (no notification) using GCM.
The relevant bits of code are as follows. Please let me know if something is amiss.
AndroidManifest.xml
<!-- GCM specific -->
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.gradians.prepwell.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.gradians.prepwell.permission.C2D_MESSAGE"/>
<receiver android:name="com.google.android.gms.gcm.GcmReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.gradians.prepwell" />
</intent-filter>
</receiver>
<service android:name=".services.MyGcmListenerService" android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service android:name=".services.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<service android:name=".services.RegisterService"
android:exported="false" />
MyGcmListenerService.java (not getting called)
public class MyGcmListenerService extends GcmListenerService {
#Override
public void onMessageReceived(String from, Bundle data) {
Log.i("debug", "onMessageReceived") ;
}
}
Rails Code
def post_potd
api_key = "AIz......-_Y"
gcm = GCM.new(api_key)
reg_ids = Device.all.map(&:gcm_token)
payload = {
data: { message: "Hello Dude!" }
}
response = gcm.send reg_ids, payload
render json: response, status: :ok
end
The JSON Response to above Rails code seems to be ok (status_code = 200, response='success' etc.). So, I am guessing GCM has no issues with the request that was posted.
But are there any other common pitfalls in GCM integration that one must watch for? And how can one check for them?
I thought I had done everything right. But I have obviously have missed something. Hence, any insights / tips / advice would be most helpful.
Thanks
Abhinav.
I am using Urban Airship for Push notification. Its works like a charm but just found that its did not sending push notification when application is not running.
How to handle this? I am sure its a common scenario and there will be a solution.
I checked many posts in stack overflow but most of them are for iOS. I want for Android
AirshipConfigOptions options = AirshipConfigOptions.loadDefaultOptions(this);
UAirship.takeOff(this, options);
Logger.logLevel = Log.VERBOSE;
PushManager.shared().setIntentReceiver(IntentReceiver.class);
PushManager.enablePush();
I have below code in Manifest Fie
<!-- REQUIRED for Urban Airship GCM-->
<receiver android:name="com.urbanairship.CoreReceiver" />
<receiver android:name="com.urbanairship.push.GCMPushReceiver" android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<!-- MODIFICATION REQUIRED - Use your package name as the category -->
<category android:name="com.itest.guide.urbanairship" />
</intent-filter>
</receiver>
<service android:name="com.urbanairship.push.PushService" android:label="Push Notification Service"/>
<service android:name="com.urbanairship.push.PushWorkerService" android:label="Push Notification Worker Service"/>
<service android:name="com.urbanairship.analytics.EventService" android:label="Event Service"/>
<!-- This is required for persisting preferences related to push and location -->
<provider android:name="com.urbanairship.UrbanAirshipProvider"
android:authorities="com.itest.mauritiustourguide.urbanairship.provider"
android:exported="false"
android:multiprocess="true" />
<!-- END OF REQUIRED ITEMS -->
<!-- OPTIONAL, if you want to receive push, push opened and registration completed intents -->
<!-- Replace the receiver below with your package and class name -->
<receiver android:name="com.itest.guide.urbanairship.IntentReceiver" />
The reason may be because of the Broadcast receiver in manifest file. Are you sure you put it? The broadcast receiver wakes the application up, and notifies when there is a push.
It doesn't look like the line the category in the BroadcastReceiver intent filter matches your app package
<category android:name="com.itest.guide.urbanairship" />