I have trouble in getting refreshed token in firebase. I have gone through documentation and followed the steps fro android exactly. In my log I find firebase connection as successful. Not sure why I am not getting the instance token. I am in initial stage and trying to get the token in logcat.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.project.application"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<application
android:allowBackup="false"
android:icon="#drawable/icon"
android:label="#string/app_name" >
<activity
android:name=".ProjectName"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#android:style/Theme.Light.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</activity>
<service
android:name=".MyInstanceIDListenerService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service
android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
</application>
</manifest>
public class MyInstanceIDListenerService extends FirebaseInstanceIdService{
private static final String TAG = "MyFirebaseIIDService";
#Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
sendRegistrationToServer(refreshedToken);
}
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
Log.d(TAG, "From: " + remoteMessage.getFrom());
String messageBody = null;
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
if (remoteMessage.getNotification() != null) {
messageBody = remoteMessage.getNotification().getBody();
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
sendNotification(messageBody);
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, SplashActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
The onTokenRefresh() method doesn't trigger the first time an app Is installed. It is only triggered by specific scenarios.
To get your token, you have to call FirebaseInstanceId.getInstance().getToken() at the start of your app (like in onCreate or something).
From the github project of FCM here
The documentation onTokenRefresh() reads :
Called if InstanceID token is updated. This may occur if the security of the previous token had been compromised. Note that this is called when the InstanceID token is initially generated so this is where you would retrieve the token.
You have to Clear Cache of you app or reinstall it in order to call this method.
fix your permission
<permission android:name="${applicationId}.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
Make sure you have correct google-service.json.
Note: token only generate once. try to clean app data to get new token
EDIT:
Or you can do iteration to get token manually in your Activity/Fragment class
FirebaseInstanceId iid = FirebaseInstanceId.getInstance();
String token = iid.getToken();
while(token == null){
iid = FirebaseInstanceId.getInstance();
token = iid.getToken();
}
Logger.info("TOKEN","==>"+token);
The onNewToken callback fires whenever a new token is generated.
/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised. Note that this is called when the InstanceID token
* is initially generated so this is where you would retrieve the token.
*/
#Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " + token);
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
sendRegistrationToServer(token);
}
To get your token, you have to call call FirebaseInstanceId.getInstance().getInstanceId()
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
#Override
public void onComplete(#NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
Log.w(TAG, "getInstanceId failed", task.getException());
return;
}
// Get new Instance ID token
String token = task.getResult().getToken();
// Log and toast
String msg = getString(R.string.msg_token_fmt, token);
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
Try deleting the google-services.json file and re-adding it.
Related
I have created one new app for push notification in https://console.firebase.google.com. I have followed all steps mentioned at last it shows -unknown app- under the head Targeting user segment.
How to test push notification in firebase?
AndroidManifest.xml
`
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
`
First, for using firebase push notifications add the dependencies
dependencies {
compile 'com.google.firebase:firebase-messaging:11.6.2' // this line must be included to use FCM
}
Add a service that extends FirebaseMessagingService
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO: Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated.
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
}
}
In your Manifest add this
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
Add a service that extends FirebaseInstanceIdService
public class FirebaseIDService extends FirebaseInstanceIdService {
private static final String TAG = "FirebaseIDService";
#Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
// TODO: Implement this method to send any registration to your app's servers.
sendRegistrationToServer(refreshedToken);
}
/**
* Persist token to third-party servers.
*
* Modify this method to associate the user's FCM InstanceID token with any server-side account
* maintained by your application.
*
* #param token The new token.
*/
private void sendRegistrationToServer(String token) {
// Add custom implementation, as needed.
}
}
Add it into the AndroidManifest.xml file, this makes sure that the service is loaded
<service android:name=".FirebaseIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
You are done implementing firebase push notifications !
Test and send your first push notification from firebase console !
hope it helped
For more info check this: https://firebase.google.com/docs/cloud-messaging/android/client?hl
happy coding !
I am trying to add notifications to my Android app using GCM. I started out by following this
tutorial and using this example project as a reference.
I followed all the steps and created all needed classes and services, but I am still not receiving any notifications.
I'm checking for Google Play Services and seem to register my device successfully on my server. I added the google-services.json file to my project and added all permissions and libraries to the project.
What is going wrong?
Did I forget anything? Do I still need to do something? I'm very confused at this point.
AndroidManifest
Permissions
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<permission android:name="com.manager_android.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
<uses-permission android:name="com.manager_android.permission.C2D_MESSAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Receiver and services
<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.manager_android" />
<!--here-->
</intent-filter>
</receiver>
<service
android:name=".Activities.GcmIntentService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name=".Activities.TokenRefreshListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
<service android:name=".Activities.RegistrationService"
android:exported="false">
</service>
Added to app build.gradle
compile 'com.google.android.gms:play-services-gcm:11.0.4'
apply plugin: 'com.google.gms.google-services'
Added to project build.gradle
classpath 'com.google.gms:google-services:3.1.0'
RegistrationService
public class RegistrationService extends IntentService {
public RegistrationService() {
super("");
}
private final String TAG = "RegistrationInfo";
#Override
protected void onHandleIntent(#Nullable Intent intent) {
registerDevice();
}
private void registerDevice() {
InstanceID myID = InstanceID.getInstance(getApplicationContext());
String registratonToken = "";
try {
registratonToken = myID.getToken(getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
log.d("Registration Token", registratonToken);
Map<String, String> params = new HashMap<>();
params.put("user_id", registratonToken);
params.put("platform", "GCM");
SaveSharedPreference.setToken(getApplicationContext(), registratonToken);
//Register token on server
Communication.post(getString(R.string.register_device_url), new RequestParams(params), new AsyncHttpResponseHandler() {
#Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
Log.i(TAG, "Registration was successful");
}
#Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Log.i(TAG, "Registration failed.");
}
});
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Couldn't register device", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
TokenRefreshListenerService
public class TokenRefreshListenerService extends InstanceIDListenerService {
#Override
public void onTokenRefresh() {
Intent i = new Intent(this, RegistrationService.class);
startService(i);
}
}
GCMIntentService
public class GcmIntentService extends GcmListenerService {
private int testId = 1234;
#Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
sendNotification(message);
}
private void sendNotification(String message) {
Intent intent = new Intent(this, MainScreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), testId, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo)
.setContentTitle("GCM Message")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis());
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(testId, notificationBuilder.build());
}
}
I start the RegistrationService as a service when the user logs in to register the device.
This documentation can help you understand about notifications.
A notification is a message you display to the user outside of your
app's normal UI. When you tell the system to issue a notification, it
first appears as an icon in the notification area. To see the details
of the notification, the user opens the notification drawer. Both the
notification area and the notification drawer are system-controlled
areas that the user can view at any time.
It also provides sample codes on every topic that you will tackle.
For GCM, you can refer to this documentation.
Google Cloud Messaging (GCM) is a free service that enables developers
to send messages between servers and client apps. This includes
downstream messages from servers to client apps, and upstream messages
from client apps to servers.
It will help you understand about the concept of GCM. Also it provides sample to start with.
It's my first time to use FCM, I created a new project in android studio, then I clicked on "tools" and I chose Firebase, I followed all the steps in this window
and I copied and pasted these blocks of code exactly in the same location as mentioned
here's my Java code
public class ZeftToken extends FirebaseInstanceIdService {
#Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d("zeft", "Refreshed token: " + refreshedToken);
}
}
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
This is my Manifest file:
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".ZeftToken">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
</application>
When I run the application I don't get the token in the logcat and it's completely missing. I don't know what's wrong.
Update
extend FirebaseMessagingService
use onNewToken instead of onTokenRefresh
getToken() method is now deprecated
#Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " + token);
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
sendRegistrationToServer(token);
}
if you want to retrieve token in you activity
use the below code .getInstanceId() instead of .getToken() method
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( MyActivity.this, new OnSuccessListener<InstanceIdResult>() {
#Override
public void onSuccess(InstanceIdResult instanceIdResult) {
String newToken = instanceIdResult.getToken();
Log.e("newToken",newToken);
}
});
I solved my solution with this function below;
public void savePushToken(){
if (!settings.hasData(Keys.pushTokenSaved) ){
FirebaseMessaging messaging = FirebaseMessaging.getInstance();
messaging.getToken().addOnSuccessListener(s -> {
Log.d("ON TOKEN",s);
pushToken = s;
JSONObject params = new JSONObject();
JSONObject info = new JSONObject();
try {
info.put("os","android");
info.put( "framework", "flutter");
info.put( "cihaz_bilgisi", new JSONObject());
params.put("token",pushToken);
params.put("device_info",info);
} catch (JSONException e) {
e.printStackTrace();
}
NetworkHelper.request(MyMethod.put, Links.savePushToken, params, false, false,
response->{
Log.d("PUSH_REQUEST",response.toString());
if (response.has("successful")||response.optString("message").contains("Integrity constraint violation: 1062 Duplicate entry")) {
settings.setBool(Keys.pushTokenSaved, true);
}
});
});
}
}
if everyting other good. (i hope you have json file provided by fcm service ? )
just move
FirebaseInstanceId.getInstance().getToken();
to for example your application or mainactivity
if u have error ( null pointer exception ) than you firebase not initialized. Check againe tutorial. And dont forget about google-json
Maybe you've already received the token in previous instance of app. The method onTokenRefresh only gets called when it is created or updated. It will not be executed in all app instances. Clear app data or uninstall app and run it again so that new token will be created and you can see it in the log.
Make sure to store the received token in shared preferences or database for future usage.
Reference Get started with Firebase
Cross check the file path of google-service.json as specified
Check the console for FCM token when you launch the app(Main Activity)
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.sdev.android.stackoverflow">
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".firebase.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service
android:name=".firebase.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
</application>
</manifest>
Main Activity
package in.sdev.android.stackoverflow;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
public class MainActivity extends AppCompatActivity {
public static String TAG = "MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "FCM token: " + refreshedToken);
}
}
Sample Firebase Package
MyFirebaseInstanceIDService.java
package in.sdev.android.stackoverflow.firebase;
import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
import static android.content.ContentValues.TAG;
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
#Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
sendRegistrationToServer(refreshedToken);
}
public void sendRegistrationToServer(String token){
Log.v("FirebaseService", "Token " + token);
}
}
MyFirebaseMessagingService.java
package in.sdev.android.stackoverflow.firebase;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static String TAG = "MyFirebaseMsgService";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// ...
// TODO(developer): Handle FCM messages 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());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + 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.
}
}
Hope this helps
I created a project on Firebase console. I had register the package on the Firebase console, But I can`t get any token from the log even hit the LOG TOKEN button in the app.
Then I try to send message with Firebase console and set to target to my app package name.I didn't get any incoming message from the log.
Code:
MainActivity.java
public class MainActivity extends AppCompatActivity {
private final String TAG = "HelloJni";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
// Example of a call to a native method
TextView tv = (TextView) findViewById(R.id.sample_text);
tv.setText(stringFromJNI());
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
Object value = getIntent().getExtras().get(key);
Log.d(TAG, "Key: " + key + " Value: " + value);
}
}
// [END handle_data_extras]
Button subscribeButton = (Button) findViewById(R.id.subscribeButton);
subscribeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// [START subscribe_topics]
FirebaseMessaging.getInstance().subscribeToTopic("news");
// [END subscribe_topics]
// Log and toast
String msg = getString(R.string.msg_subscribed);
Log.d(TAG, msg + ", " + FirebaseInstanceId.getInstance().getToken());
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
}
RegistrationIntentService.java
public class RegistrationIntentService extends IntentService {
private static final String TAG = "RegIntentService";
public RegistrationIntentService() {
super(TAG);
}
#Override
protected void onHandleIntent(Intent intent) {
String token = FirebaseInstanceId.getInstance().getToken();
Log.i(TAG, "FCM Registration Token: " + token);
}
}
MyFirebaseInstanceIDService.java
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
#Override
public void onCreate() {
super.onCreate();
Log.e(TAG, "oncreate.........");
}
/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised. Note that this is called when the InstanceID token
* is initially generated so this is where you would retrieve the token.
*/
// [START refresh_token]
#Override
public void onTokenRefresh() {
Log.e(TAG, "onTokenRefresh call...");
// Get updated InstanceID token.
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
sendRegistrationToServer(refreshedToken);
}
// [END refresh_token]
/**
* Persist token to third-party servers.
*
* Modify this method to associate the user's FCM InstanceID token with any server-side account
* maintained by your application.
*
* #param token The new token.
*/
private void sendRegistrationToServer(String token) {
Log.d(TAG, " sendRegistrationToServer Refreshed token: " + token);
// TODO: Implement this method to send token to your app server.
}
}
MyFirebaseMessagingService.java
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
#Override
public void onCreate() {
super.onCreate();
Log.e(TAG, "oncreate..........");
}
/**
* Called when message is received.
*
* #param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
// [START receive_message]
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// [START_EXCLUDE]
// There are two types of messages data messages and notification messages. Data messages are handled
// here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
// traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
// is in the foreground. When the app is in the background an automatically generated notification is displayed.
// When the user taps on the notification they are returned to the app. Messages containing both notification
// and data payloads are treated as notification messages. The Firebase console always sends notification
// messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
// [END_EXCLUDE]
// TODO(developer): Handle FCM messages 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());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + 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.
}
// [END receive_message]
/**
* Create and show a simple notification containing the received FCM message.
*
* #param messageBody FCM message body received.
*/
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);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name="com.example.hellojni.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name="MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service android:name="RegistrationIntentService" ></service>
</application>
OnCreate() for MyFirebaseInstanceIDService and MyFirebaseMessagingService was not called, and the token returns null.
The log:
02-20 18:05:28.273 23322-23322 W/InstanceID/Rpc: Failed to resolve REGISTER intent, falling back
02-20 18:05:28.273 23322-23322 W/InstanceID/Rpc: Both Google Play Services and legacy GSF package are missing
02-20 18:05:28.273 23322-23322 W/InstanceID/Rpc: Failed to resolve REGISTER intent, falling back
02-20 18:05:28.273 23322-23322 W/InstanceID/Rpc: Both Google Play Services and legacy GSF package are missing
02-20 18:05:28.273 23322-23322 D/HelloJni: Subscribed to news topic, null
The device or emulator you are testing on does not have Google Play Services or the Google Service Framework (GSF) installed. Most of the Firebase APIs use the capabilities of Google Play Services and will not run if it is not present on the device.
put this at last after dependency in your app level gradle.apply plugin: 'com.google.gms.google-services'
and put this into your project level gradle.classpath 'com.google.gms:google-services:3.0.0'
make sure you have puted google-service.json in app module.
you have to add .
apply plugin: 'com.google.gms.google-services'
in last line in your gradle file and .
classpath 'com.google.gms:google-services:3.0.0'
in your project level file . IntentService service class will called only ones when your app intralled so delete app every time and and intrall every time when you want to generate tocken
I see the reason. I run the app with Android phone made in China, and these phones has no google play service and store.
I want to send a notification to the user when a task is assigned (i.e on a button click). When a task is assigned to the user I change some values in my server(i.e i'm performing a post method) I would like to know how to achieve this.
I have reached till registering the user when he logs in to my app & i'm storing users token id into my server I have also gone through google developers guide and have registered my app and obtained senderid and serverapikey. I will post the code till where I have reached please help me on how to proceed further.
LoginActivity
private void checkUserRegistrationToken() {
String url = URLMap.getGcmtokenUrl("gcmtoken_url");
employeeId = LoggedInUserStore.getLoggedInEmployeeId(getApplicationContext());
companyId = LoggedInUserStore.getLoggedInCompanyId(getApplicationContext());
url = url.replace("{eid}", employeeId).replace("{cid}", companyId);
final StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject jObj = new JSONObject(response);
tokenId = jObj.getString("TokenId");
if (tokenId.equals("null")) {
registerInBackground();
}
} catch (JSONException e) {
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
System.out.println("Error===" + error.toString());
}
});
request.setRetryPolicy(new VolleyRetryPolicy().getRetryPolicy());
RequestQueue queue = ((VolleyRequestQueue) getApplication()).getRequestQueue();
queue.add(request);
}
private void registerInBackground() {
new AsyncTask<Void, Void, String>() {
#Override
protected String doInBackground(Void... params) {
String msg = "";
try {
if (gcmObj == null) {
gcmObj = GoogleCloudMessaging.getInstance(LoginActivity.this);
}
tokenId = gcmObj.register(String.valueOf(R.string.gcm_defaultSenderId));
msg = "Registration ID:" + tokenId;
if (new ServiceManager(getApplicationContext()).isNetworkAvailable() && checkPlayServices()) {
String storeUrl = URLMap.getGcmtokenPostUrl();
employeeId = LoggedInUserStore.getLoggedInEmployeeId(getApplicationContext());
companyId = LoggedInUserStore.getLoggedInCompanyId(getApplicationContext());
HashMap<String, String> map = new HashMap<String, String>();
map.put("EmployeeId", employeeId);
map.put("CompanyId", companyId);
map.put("TokenId", tokenId);
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, storeUrl, new JSONObject(map), new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.i(TAG, "Token has been posted in server!");
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.i(TAG, "Error posting token into server!!");
}
});
request.setRetryPolicy(new VolleyRetryPolicy().getRetryPolicy());
RequestQueue queue = ((VolleyRequestQueue) getApplication()).getRequestQueue();
queue.add(request);
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Token Mesage=" + msg);
return msg;
}
#Override
protected void onPostExecute(String s) {
}
}.execute();
}
As you can see that I'm storing token id of user into my server.
Now I have also created a GcmBroadcastReceiver class
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(),
NotificationService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
and I have also created a NotificationService class which has on MessageReceived method
public class NotificationService extends GcmListenerService {
public static final int notifyID = 9001;
public static final String appname = "FM Ninja";
NotificationCompat.Builder builder;
public NotificationService() {
// super("GcmIntentService");
}
#Override
public void onMessageReceived(String from, Bundle data) {
Intent resultIntent = null;
PendingIntent resultPendingIntent;
resultIntent = new Intent(this, HomeActivity.class);
resultPendingIntent = PendingIntent.getActivity(this, 0,
resultIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder mNotifyBuilder;
NotificationManager mNotificationManager;
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle("title")
.setTicker("New Message !")
.setContentText("first message please")
.setSmallIcon(R.mipmap.cms_launch_icon);
// Set pending intent
mNotifyBuilder.setContentIntent(resultPendingIntent);
// Set Vibrate, Sound and Light
int defaults = 0;
defaults = defaults | Notification.DEFAULT_LIGHTS;
defaults = defaults | Notification.DEFAULT_VIBRATE;
defaults = defaults | Notification.DEFAULT_SOUND;
mNotifyBuilder.setDefaults(defaults);
mNotifyBuilder.setAutoCancel(true);
// Post a notification
mNotificationManager.notify(notifyID, mNotifyBuilder.build());
}
}
The problem is even after adding permission in Manifeast when a call is made from server onMessageReceived is never getting executed Please help me.
I will also post manifeast file
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!--GCM Permissions-->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.google.android.c2dm.permission.SEND"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<permission android:name="com.six30labs.cms.permission.C2D_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.six30labs.cms.permission.C2D_MESSAGE" />
<application
android:name="com.six30labs.cms.general.VolleyRequestQueue"
android:allowBackup="true"
android:icon="#mipmap/cms_launch_icon"
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.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity android:name=".LoginActivity" />
<activity android:name=".ForgotPassword" />
<activity android:name=".NoInternet" />
<activity android:name=".HomeActivity"/>
<activity android:name=".ComplaintDetailsSupervisor" />
<activity android:name=".ComplaintDetailsEmployee" />
<activity android:name=".NavBarProfile" />
<activity android:name=".ComplaintDetailsWorker" />
<activity android:name=".AssignedDetailSupervisor" />
<activity android:name=".AcceptedComplaintDetailsWorker" />
<activity android:name=".VerifyDetailSupervisor"/>
<activity android:name=".ManagerComplaintListActivity"/>
<!--<service android:name="com.six30labs.cms.storage.RegistrationIntentService"
android:exported="false"/>-->
<receiver
android:name="com.six30labs.cms.general.GcmBroadcastReceiver"
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.six30labs.cms"/>
</intent-filter>
</receiver>
<!-- Register Service -->
<service android:name="com.six30labs.cms.general.NotificationService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
</application>
Server side script
public class GCMNotification
{
private CMSEntities db = new CMSEntities();
static string gcmid = "AIzaSyB7HSIF1RIvkyCnpP6KtYiy6wQ-s6YBscY";
public void AssignEmpNotification(string employeeid)
{
long id = Convert.ToInt64(employeeid);
PushBroker pushBroker = new PushBroker();
pushBroker.RegisterGcmService(new GcmPushChannelSettings(gcmid));
pushBroker.QueueNotification(new GcmNotification().ForDeviceRegistrationId("APA91bGElkVodLyubuMM90TEnfUMab0Fs6JudsjXcgIUrTrT8Zk3GezKYWc9w2gGs6pzLLq_nPSZCXU30M5iYKdRJcKZnkafWuwhnihZQ88vcwUrKhiQn6eWSqGrLCeHFblVT09IR7jy")
.WithJson(#"{""message"":""Hi Hello" + "wsfdasd" + #""",""title"":""title" + "vendorBids" + #""",""Bidsid"":""" + "1" + #""",""Eventdate"":""" + "2/2/2016" + #""",""vendorname"":""" + "name" + #"""}"));
pushBroker.StopAllServices();
}
}
The server needs to send http post request
to "https://android.googleapis.com/gcm/send" along with list of
registration ids and message as body data.
The body data is a combination of :
a. registration ids (which is a list of array)
b. message
The header is a combination of :
a. Content-Type
b. Authorization (key:project id)
List regIds = new ArrayList();
//add regIds to this list : regIds.add("value");
JSONObject data = new JSONObject ();
data.put("registration_ids", regIds);
data.put("Message", "Hello");
ApiKey is the value we received during project created in google store.
Map headers = new HashMap();
headers.put("Content-Type", "application/json");
header.put("Authorization","key=ApiKey");
After sending http post request. The google gcm server will send
the message to all users whose registration id is mentioned in http
request.
The android mobile will receive a notification from gcm server as response
which will be handled by GCMBroadcastReceiver.
Now NotificationService class will be invoked which will extent
IntentService.
In this class ,
#Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty() &&
GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
showNotification(intent.getStringExtra("Message"));
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
This method is calling "showNotification" method which has a string
parameter.
Use this message and show in notification using NotificationManager.
Hope this helps :)
Start logging the response that the server is getting after sending the notification payload to google servers. If there is a problem with the device token, you will get a detailed error like "Not Registered".
If the there is no problem in token, google server will respond you with success and message id.
Once you are assured that there is no problem with the device token, then we can think about the issues in the device code.
Other probable issue I can think of is that the SHA hash that you have added in the google developer console is not matching with the SHA hash of the keystore that you have signed the app with.
Add the SHA hash of debug.keystore as well as the production signing keystore.