Can't acquire FCM registration token - java

I'm trying to get the FCM registration token or the device token in short to send push notifications to my application from my server.
The thing is I tried to get it like the code shown below but nothing happens even after uninstalling and reinstalling the apps. I'm already made sure that my Tag manager is working and also all the services needed already set in AndroidManifests.xml.
For information, I already test the Push Notifications from the firebase site and it's working just fine. But that's not what I'm aiming for. I don't understand why I can send the notifications from Firebase but can't see/acquire the FCM registration token on my Android Studio console. Is there something I missed here. I'm a beginner in this kind of thing and have been stucked with this problem for about 1 week already and at my wits end here. Hope somebody can help me. Thanks in advance for your time.
public class FirebaseIDService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIdService";
public String tok;
#Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
// Instance ID token to your app server.
//sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
// TODO: Implement this method to send token to your app server.
}
}
AndroidManifests.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.asii_developer.asiisupport">
<uses-permission android:name="android.permission.INTERNET" />
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="#mipmap/planete"
android:label="#string/app_name"
android:largeHeap="true"
android:roundIcon="#mipmap/planete"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<!--
<service
android:name=".DeviceToken">
<intent-filter>
</intent-filter>
</service>
-->
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="#color/colorPrimary" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity android:name=".PageBienvenueActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".PagedAccueilActivity" />
<activity android:name=".FormActivity" />
<activity android:name=".RecapActivity" />
<activity android:name=".PageDeConnexion" />
<activity android:name=".mot_de_passeActivity" />
<activity
android:name=".Liste_des_ticketsActivity"
android:windowSoftInputMode="stateHidden|adjustPan" />
<activity
android:name=".ConversationActivity"
android:windowSoftInputMode="stateHidden|adjustPan" />
<activity android:name=".NotificationsActivity">
<intent-filter>
<action android:name="notifActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<service
android:name=".MyFirebaseMessagingService"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="com.google.Firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".FirebaseIDService"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.google.Firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
</application>
</manifest>
MyFireBaseMessagingService.xml
package com.example.asii_developer.asiisupport;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
Intent intent = new Intent(this, NotificationsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1,
intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new
NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.arrow)
.setContentTitle("Message")
.setContentText(remoteMessage.getNotification().getBody())
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notificationBuilder.build());
}
}

Try this:
// [START refresh_token]
#Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
Toast.makeText(this, "Refreshed token: " + refreshedToken, Toast.LENGTH_LONG).show();
// Implement this method to send token to your app's server
sendRegistrationToServer(refreshedToken);
}
// [END refresh_token]
And one more thing:
You need to call sendRegistrationToServer() method which will update
token on server, if you are sending push notifications from server.
NOTE:
New Firebase token is generated (onTokenRefresh() is called) when:
The app deletes Instance ID
The app is restored on a new device
Theuser uninstalls/reinstall the app
The user clears app data.
Hope this helps! Good luck!

You need to call FirebaseInstanceId.getToken() first (probably in your MainActivity or any corresponding initial activity.
onTokenRefresh() only triggers later in the beginning if and only if the initial call to FirebaseInstanceId.getToken() return null. As mentioned in the FCM docs:
The onTokenRefreshcallback fires whenever a new token is generated, so calling getToken in its context ensures that you are accessing a current, available registration token. FirebaseInstanceID.getToken() returns null if the token has not yet been generated.

Related

How to dispatch intent to MainActivity from another activity? (react native)

I've created a custom deep link dispatcher in Java within a react native project.
The issue I'm having is that I can't get MainActivity to start from my dispatcher, and I can't quite figure out why.
I have AndroidManifest set up so that the deep link is captured in LinkDispatcherActivity effectively, but the startActivity(dispatchedIntent) never reaches MainActivity, which is where I assume it needs to go. Ordinarily in AndroidManifest.xml, deep links would be sent to MainActivity).
It does open the app, but I assume it's because LinkDispatcherActivity is part of the app? Not sure, if I say something dumb it's because this is only my 3rd day in a row writing Java 😰
Below is my code.
package com.example.app;
//Inspired by https://github.com/justeat/Android.Samples.Deeplinks, Licensed under Apache 2.0
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import net.openid.appauth.RedirectUriReceiverActivity;
public class LinkDispatcherActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
dispatchIntent(getIntent());
} catch (IllegalArgumentException iae) {
// Malformed URL
if (BuildConfig.DEBUG) {
Log.e("Deep links", "Invalid URI", iae);
}
} finally {
// Always finish the activity so that it doesn't stay in our history
finish();
}
}
public void dispatchIntent(Intent intent) {
final Uri uri = intent.getData();
final String host = uri.getHost().toLowerCase();
if (uri == null) throw new IllegalArgumentException("Uri cannot be null");
// Default intent
Intent dispatchedIntent = new Intent(LinkDispatcherActivity.this, MainActivity.class);
dispatchedIntent.setData(uri);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Log.d(dispatchedIntent.getDataString(), "dispatchIntent: intent string");
// Auth intent only, this is why I need the dispatcher
if ("login".equals(host)) {
dispatchedIntent = new Intent(this, RedirectUriReceiverActivity.class);
dispatchedIntent.putExtra("requestCode", 0);
Log.d("Login", "mapAppLink: ");
startActivityForResult(dispatchedIntent, 0);
return;
}
Log.d("Default", "mapAppLink: ");
startActivity(dispatchedIntent);
}
}
This is what my AndroidManifest.xml looks like:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
package="com.example.app">
<!-- < Only if you're using GCM or localNotificationSchedule() > -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="${applicationId}.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
<!-- < Only if you're using GCM or localNotificationSchedule() > -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name=".MainApplication"
android:allowBackup="false"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:theme="#style/AppTheme"
android:requestLegacyExternalStorage="true">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="<redacted>" />
<!-- < Notification Services > -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#mipmap/ic_notification" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
tools:replace="android:resource"
android:resource="#color/colorAccent" />
<!-- Change the value to true to enable pop-up for in foreground on receiving remote notifications (for prevent duplicating while showing local notifications set this to false) -->
<meta-data android:name="com.dieam.reactnativepushnotification.notification_foreground"
android:value="true"/>
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
tools:replace="android:value"
android:value="rn-push-notification-channel-id" />
<meta-data android:name="com.dieam.reactnativepushnotification.notification_channel_name" android:value="Default Channel"/>
<meta-data android:name="com.dieam.reactnativepushnotification.notification_channel_description" android:value="Default channel for push notifications"/>
<meta-data
android:name="com.dieam.reactnativepushnotification.notification_color"
android:resource="#color/colorAccent" />
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationActions" />
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
<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"/>
</intent-filter>
</receiver>
<!-- START: Add this-->
<service
android:name=".MainNotificationService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<receiver
android:name="com.intercom.reactnative.RNIntercomPushBroadcastReceiver"
tools:replace="android:exported"
android:exported="true"/>
<activity
android:name=".MainActivity"
android:configChanges="keyboard|keyboardHidden|screenSize|uiMode"
android:launchMode="singleTask"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</activity>
<activity android:name=".LinkDispatcherActivity"
tools:node="replace">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="example"
android:host="login" />
</intent-filter>
</activity>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
</application>
</manifest>
Edit: for the intent, I've tried:
new Intent(getApplicationContext(), MainActivity.class)
new Intent(reactContext, MainActivity.class) (extending ReactActivity instead of Activity)
new Intent(this, MainActivity.class)
new Intent("MainActivity")
Try adding RedirectUriReceiverActivity to the manifest - I couldn't see it in there.
<activity android:name="com.facebook.react.devsupport.RedirectUriReceiverActivity" />
This is what I ended up with for the LinkDispatcherActivity. To summarize, what I did was dispatch the intents that the deep activities were eventually supposed to invoke directly, instead of trying to forward the intent. In other words, if you're trying to pass an intent from one activity to another, maybe don't and consider dispatching the activity/function/intent that you want to run afterwards. I still think forwarding the intent should have worked, but this is what ended up working.
In order to accomplish the above, I:
Called a function within the AppAuth package that generates an intent, and dispatch it
Used ReactContext to emit the url listener event that React Native uses to respond to deep links.
Below is the code:
//Inspired by https://github.com/justeat/Android.Samples.Deeplinks, Licensed under Apache 2.0
package com.example.app;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import net.openid.appauth.AuthorizationManagementActivity;
import com.facebook.react.ReactActivity;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
public class LinkDispatcherActivity extends ReactActivity {
#Override
protected void onResume() {
super.onResume();
try {
dispatchIntent(getIntent());
} catch (IllegalArgumentException iae) {
// Malformed URL
if (BuildConfig.DEBUG) {
Log.e("Deep links", "Invalid URI", iae);
}
} finally {
// Always finish the activity so that it doesn't stay in our history
finish();
}
}
public void dispatchIntent(Intent intent) {
final Uri uri = intent.getData();
final String host = uri.getHost().toLowerCase();
if (uri == null) throw new IllegalArgumentException("Uri cannot be null");
// Default intent, e.g. settings, fitbit
// wait for react context
ReactInstanceManager reactInstanceManager = getReactInstanceManager();
ReactContext reactContext = reactInstanceManager.getCurrentReactContext();
Intent dispatchedIntent = new Intent(getApplicationContext(), MainActivity.class);
dispatchedIntent.setData(uri);
dispatchedIntent.putExtra("url", uri.toString());
dispatchedIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
// Auth intent only
if ("login".equals(host)) {
Intent authIntent = AuthorizationManagementActivity.createResponseHandlingIntent(getApplicationContext(), uri);
startActivity(authIntent);
return;
}
// Below from https://stackoverflow.com/questions/48445010/send-data-from-android-activity-to-react-native?rq=1
WritableMap uriMap = Arguments.createMap();
uriMap.putString("url", uri.toString());
if(reactContext != null) {
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("url", uriMap);
// If there's no context, create a listener to wait for it
} else {
reactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
#Override
public void onReactContextInitialized(ReactContext context) {
context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("url", uriMap);
reactInstanceManager.removeReactInstanceEventListener(this);
}
});
}
}
}

How to generate a token with firebase (FCM) and share

I am trying to build an app, but I face a problem when I want to generate TOKEN with Firebase, because in the past, I used an old method provided by Google but with the new method I can't generate the token and share.
The previous method that worked for me was to send the token, now deprecated is following:
import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
#Override
public void onTokenRefresh() {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
storeToken(refreshedToken);
}
private void storeToken(String token) {
//saving the token on shared preferences
SharedPreference.getInstance(getApplicationContext()).saveDeviceToken(token);
}
}
And this is the new method, but it doesn't work. My token not is generated and cannot be shared:
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseInstanceIDService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseIIDService";
#Override
public void onNewToken(String token) {
super.onNewToken(token);
Log.d("NEW_TOKEN",token );
storeToken(token);
}
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
}
private void storeToken(String token) {
//saving the token on shared preferences
SharedPreference.getInstance(getApplicationContext()).saveDeviceToken(token);
}
}
My AndroidManifest file:
<application
android:usesCleartextTraffic = "true"
android:name=".app.AppController"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:fullBackupContent="#xml/backup_descriptor">
<activity android:name=".ge_inicio"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
Thanks for the help
Use only this code inside your AndroidManifest
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
and delete below method
<service android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
And Clear your app data or reinstall your app to get the new token
If you want to get id in other activities, read this answer
Why don't you try this way to generate token just put below code in your MainActivity.
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(this,
instanceIdResult -> {
String newToken = instanceIdResult.getToken();
SharedPreference.getInstance(getApplicationContext()).saveDeviceToken(token)
Firebase Id create once when app newly install or create id when app cache memory cleared. You need to uninstall and reinstall it before debug .

Firebase Foreground Notifications Not Showing

For some reason my Firebase notifications are not showing in the foreground. They work fine in the background however, and I followed what Anroid Studio told me to add as well as this tutorial example on github
Judging by the Run window, it is receiving the notification but not sending in foreground
Here is my service code for sending the notification
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import static android.support.constraint.Constraints.TAG;
public class FirebaseNotificationsRecieverService extends FirebaseMessagingService {
private static final String TAG = FirebaseNotificationsRecieverService.class.getName();
public FirebaseNotificationsRecieverService() {
}
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// ...
System.out.println("RECIEVED");
// TODO(developer): Handle FCM messages here.
// Not getting messages here? See why this may be: (there was a link here)
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
if (/* Check if data needs to be processed by long running job */ true) {
// For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
//scheduleJob();
} else {
// Handle message within 10 seconds
handleNow();
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
sendNotification(remoteMessage.getNotification().getBody());
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
private void handleNow() {
Log.d(TAG, "Short lived task is done.");
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.wlmaclogo)
.setContentTitle(getString(R.string.fcm_message))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.starenkysoftware.macapp">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:usesCleartextTraffic="true">
<service
android:name=".FirebaseNotificationsRecieverService"
android:enabled="true"
android:exported="true"></service>
<service android:name="com.google.firebase.messaging.FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Replace the manifest as below:-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.starenkysoftware.macapp">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:usesCleartextTraffic="true">
<service android:name=".FirebaseNotificationsRecieverService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
The intent filter com.google.firebase.MESSAGING_EVENT should be placed in your service, which you are using to get the notifications.
Remember to remove the existing messaging service from the AndroidManifest.xml. If you leave the FireBaseMessagingService in the Manifest. your newly defined service doesn't get picked up.
E.g. remove the following:
<service
android:name="com.google.firebase.messaging.FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
and add the following:
<service
android:name=".MyFirebaseNotificationService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>

Push notifications doesn't work properly when application is closed in Android Studio

I'm developing an application in which I send notifications through Firebase. I've followed several tutorials in which the idea is to open an activity when I press the notification. This action works correctly as long as the App is open, but if it is closed or "minimized" and press the notification it is opened from the default activity, which in my case would be the SplashScreen, I really don't explain why it doesn't work. Is there anything you can recommend?
The following would be the class we know as MyFirebaseMessagingService. As you can see when I press the notification, it takes me to NotificationActivity.class and it does... but only if the application is open.
package com.asecum.com.campussicapp;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Created by asecum5 on 24/08/17.
*/
public class Firebase_NotificationService extends FirebaseMessagingService {
private static final String TAG = "NOTICIAS";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
/*String from = remoteMessage.getFrom();
Log.d(TAG, "Mensaje recibido de: " + from);
if(remoteMessage.getNotification() != null) {
Log.d(TAG, "Notification: " + remoteMessage.getNotification().getBody());
showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}
if(remoteMessage.getData().size()>0){
Log.d(TAG, "Data: " + remoteMessage.getData());
}*/
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Data: " + remoteMessage.getData());
try {
JSONObject data = new JSONObject(remoteMessage.getData());
String jsonMessage = data.getString("extra_information");
Log.d(TAG, "onMessageReceived: \n" + "Extra information: " + jsonMessage);
} catch (JSONException e) {
e.printStackTrace();
}
}
if (remoteMessage.getNotification() != null) {
String title = remoteMessage.getNotification().getTitle();
String message = remoteMessage.getNotification().getBody();
showNotification(title, message);
}
}
private void showNotification(String title, String body) {
Intent intent = new Intent(this, NotificationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_home_black_24dp)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setVibrate(new long[]{100, 250, 100, 250, 100, 250})
.setSound(soundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
The next is the manifest... Could be possible that i forgot some permission?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.asecum.com.campussicapp">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".SplashScreen"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NotificationActivity">
<intent-filter>
<action android:name="NOTIFICATIONACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<service
android:name=".Firebase_InstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service
android:name=".Firebase_NotificationService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="#color/colorAccent" />
</application>
</manifest>
This is what I added in gradle
Module's build.gradle
dependencies {
...
compile 'com.google.firebase:firebase-core:10.2.6'
compile 'com.google.firebase:firebase-messaging:10.2.6'
testCompile 'junit:junit:4.12'
}
apply plugin: `com.google.gms.google-services`
Project's build.gradle
dependencies {
...
classpath 'com.google.gms:google-services:3.1.0'
}
I think this is caused by the difference between "data" style cloud messages and "notification" style cloud messages in Firebase.
For a notification style message:
FCM automatically displays the message to end-user devices on behalf
of the client app.
Also note that the firebase console can send only the notification style of message.
Could it be that this notification style cloud message arrives and is being handled automatically by the android system and not by your app (when the app is closed or in the background)?
If that is the case, then the system will open the app's default activity (splash screen) when the user clicks the notification.
As an alternative, you can send a "data" style cloud message which will be handled by your app instead of by the system. Then when the user clicks the notification, it will execute the code defined in your .Firebase_NotificationService
From a Mac command line, you can use an a script like this to send a data style message through Google's servers to a particular app instance id:
curl -X POST \
--Header "Authorization: key=<get authorization key from Firebase console - Project Settings - Web API Key>” \
--Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send \
-d " \
{ \
\"to\”:\”<get id for your particular android device app instance from Firebase Instance ID service in your app>\”, \
\"data\": \
{ \
\"title\":\" test title here \", \
\"body\":\" test body here \"
}, \
\"priority\": \"normal\" \
}"
Notice that there is no "notification" block in the JSON.
See also this and this

Starting a background service when the OS boots

I'm trying to start my background service when the device boots, but nothing is happening. This is my code:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.test"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Autostart.java
package it.test;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class Autostart extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
Intent intent = new Intent(arg0, AppService.class);
arg0.startService(intent);
Log.e("it.Test", "***** SERVER STARTING CALLED *****");
}
}
AppService.java
package it.test;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class AppService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onDestroy() {
Toast.makeText(this, "My AppService Stopped", Toast.LENGTH_LONG).show();
Log.e("it.Test", "***** SERVICE STOPPED *****");
}
#Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My AppService Started", Toast.LENGTH_LONG).show();
Log.e("it.Test", "***** SERVICE STARTED *****");
/*
MainActivity.calc();
MainActivity.save();
*/
}
}
When my phone boots I don't see any toast (I don't see either the "My AppService Started" nor the "My AppService Stopped" toast) and nothing gets logged by the application...
Anyone knows what I am doing wrong??
Thanks!
EDIT: Solution to the problem
After seeing the post Trying to start a service on boot on Android linked in an answer and after many debugging I found the solution to my problem :) Thank to everyone!
Modification to AndroidManifest.xml
Added inside <application>:
<receiver android:name="it.test.Autostart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name="it.test.AppService"
android:enabled="true" />
Added also:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
[...]
android:installLocation="internalOnly">
Modifications to AppService.java
To make the service work, I had to override in the AppService class onStartCommand instead of onStart, so I completely removed the method onStart from my class.
New onStartCommand:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "My AppService Started", Toast.LENGTH_LONG).show();
Log.e("it.Test", "***** SERVICE STARTED ***** from onStartCommand");
MainActivity.calc(this);
MainActivity.save();
return Service.START_REDELIVER_INTENT;//todo I'm not sure about what I have to return
}
Your broadcast receiver is not registered at AndroidManifest.xml
You need to register the broadcast receiver:
<receiver android:name=".<RECEIVER_NAME>">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
If your app is installed to external storage it won't receive BOOT_COMPLETE broadcast message. To prevent this you can install your application in internal storage. you can do this just adding this line in AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="internalOnly"
... >
Source: https://stackoverflow.com/a/32938071/1549700
You have you register your broadcast receiver and service under the <application> tag
<!-- [START service_listener] -->
<service
android:name=".it.test.AppService"
android:enabled="true" />
<!-- [END service_listener] -->
<!-- [START broadcast_receiver] -->
<receiver android:name=".it.test.Autostart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<!-- [END broadcast_receiver] -->
You need to register the broadcast receiver:
<receiver android:name=".Autostart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
You need this in your manifest file:
<receiver android:name=".it.test.Autostart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Have you register broadcast receiver in manifest file and also also give boot completed permission in your broadcast receiver, if any problem then ask in comment, your code is perfect.

Categories

Resources