Data sent through Data API from mobile app is not received in ondatachange of listenerservice in wearable emulator.I can send a notification though, which indicates both are connected.Below my code,
DataAPI call (mobile)
PutDataMapRequest putDataMapRequest = PutDataMapRequest.create(SharedConstants.START_FREE_RUN);
putDataMapRequest.getDataMap().putString(SharedConstants.PROGRAM_TYPE,totalCountUpTimer);
PutDataRequest request = putDataMapRequest.asPutDataRequest();
Wearable.DataApi.putDataItem(mGoogleApiClient, request)
.setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
#Override
public void onResult(DataApi.DataItemResult dataItemResult) {
if (!dataItemResult.getStatus().isSuccess()) {
Log.e(TAG, "buildWatchOnlyNotification(): Failed to set the data, "
+ "status: " + dataItemResult.getStatus().getStatusCode());
} else {
Log.d(TAG,"SuccessFully sent notification");
}
}
});
After which I am getting "successfully sent" log message.
Below is ListenerService in wear,
public class ListenerService extends WearableListenerService {
private static final String TAG = ListenerService.class.getSimpleName();
#Override
public void onDataChanged(DataEventBuffer dataEvents) {
super.onDataChanged(dataEvents);
Log.d(TAG, "dchanged" + dataEvents);
}
#Override
public void onMessageReceived(MessageEvent messageEvent) {
Log.v(TAG, "onMessageReceivedWear: " + messageEvent);
if (SharedConstants.START_FREE_RUN.equals(messageEvent.getPath())) {
// Request for this device open the attraction detail screen
// to a specific tourist attraction
String Distance = new String(messageEvent.getData());
Log.d("ListenerService",Distance);
}
}
}
and my service declaration in Android manifest,
<service android:name=".services.ListenerService">
<intent-filter>
<action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
<action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
<data android:scheme="wear" android:host="*" android:pathPrefix="*" />
</intent-filter>
</service>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
Did not add any permissions in wearable module.Do suggest.
Use android:pathPattern=".*" instead of android:pathPrefix="*"
Related
In my project, I'm using two libs to handle two different types of push notifications: Localytics and react-native-push-notification.
I have a custom FirebaseMessagingService that checks if it's a Localytics push then let the Localytics handle it via their provided methods. But if it's not a Localytics push, I need to pass this push data to react-native-push-notification's RNPushNotificationListenerService which is also a FirebaseMessagingService.
I'm attempting to start the RNPushNotificationListenerService but it doesn't seem to be starting because it never sends a push. I have tried setting breakpoints too but no luck.
AndroidManifest.xml
<!-- push notifications -->
<service android:name=".fcm.CustomFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<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" />
</intent-filter>
</receiver>
<service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationRegistrationService"/>
<service
android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<!-- push notifications end -->
CustomFirebaseMessagingService.java
public class CustomFirebaseMessagingService extends FirebaseMessagingService {
public CustomFirebaseMessagingService() {
super();
}
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
try {
if (!Localytics.handleFirebaseMessage(data)) {
forwardPushNotification(remoteMessage);
}
} catch (Exception e) {
Timber.e(e, "Failed to extract Push Message", remoteMessage.getMessageId());
}
}
#Override
public void onDeletedMessages() {
super.onDeletedMessages();
}
#Override
public void onMessageSent(String msgId) {
super.onMessageSent(msgId);
}
#Override
public void onSendError(String msgId, Exception e) {
super.onSendError(msgId, e);
}
#Override
public void onNewToken(String token) {
super.onNewToken(token);
Localytics.setPushRegistrationId(token);
}
private void forwardPushNotification(RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
data.put("from", remoteMessage.getFrom());
Intent intent = new Intent(this, RNPushNotificationListenerService.class);
Bundle extraData = new Bundle(data.size());
for (Map.Entry<String, String> entry : data.entrySet()) {
extraData.putString(entry.getKey(), entry.getValue());
}
intent.putExtras(extraData);
startService(intent);
}
}
I have a feeling I'm not starting the service correctly?
I am also aware that AndroidManifest will ignore the declaration of com.google.firebase.MESSAGING_EVENT for RNPushNotificationListenerService.
I have tried getting the individual libraries working without my custom FirebaseMessagingService and they both work correctly. I just need to figure out how to start RNPushNotificationListenerService when I have both setup with my custom FirebaseMessagingService.
This is what I have inside my AndroidManifest:
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name="io.smooch.core.service.SmoochService" />
<service android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
I am using version: implementation 'com.google.firebase:firebase-messaging:17.0.0'
This is how my FirebaseMessagingService looks like:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public void onMessageReceived(final RemoteMessage message) {
Log.i("", "MyFirebaseMessagingService onMessageReceived");
final Map data = message.getData();
FcmService.triggerSmoochNotification(data, this);
final Map bundle = data;
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
#Override
public void run() {
Log.i("", "GLOBAL intances before GCM:" + Realm.getGlobalInstanceCount(PSApplicationClass.Config));
try {
String title = getString(R.string.alert_title_warning) + " " + getString(R.string.trips_remaining_free, user.getTrips_left());
String message = getString(R.string.alert_freemium_upgrade);
Notification.generateNotificationStandard(MyFirebaseMessagingService.this, title, message, null, null, false, false);
} catch (Exception e) {
Utils.appendLog("GCMIntentService onMessageReceived error: " + e.getMessage(), "E", Constants.PUSH_NOTIFICATIONS);
}
}
});
}
}
And basically I put a breakpoint at the onMessageReceived of this function.
If the server sends a notification to my app, it will be received by this Service, and I can handle it as needed.
BUT. If I use the FirebaseConsole -> GROW -> Could Messaging -> I create a new Message and I send it. My app will get the notifications. but it will be created "automatically" without entering my onMessageReceived method.
How can I force it to do so?
This is the data that I send via the console:
https://s3.amazonaws.com/uploads.hipchat.com/39260/829560/7Pp0KJIErZ8XVZZ/upload.png
What am I doing wrong?
EDIT:
Like I said, I get notifications, but the notifications are being created automatically instead of going through my FirebaseMessaging Service.
I did manage to change the icon like this:
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/notification_icon" />
But I still need to run other code when I get it, not just show it. and that I do not know how
Hi there I'm building the chat app using firebase and I would like to push notification when a user send message to another user with FCM.
Any help please
add compile 'com.google.firebase:firebase-messaging:11.8.0' to your app/build.gradle file.
and below code to your AndroidManifest.xml
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service
android:name=".MyFirebaseInstanceIdService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
MyFirebaseMessagingService.java
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFMService";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Handle data payload of FCM messages.
Log.d(TAG, "FCM Message Id: " + remoteMessage.getMessageId());
Log.d(TAG, "FCM Notification Message: " +
remoteMessage.getNotification());
Log.d(TAG, "FCM Data Message: " + remoteMessage.getData());
}
}
MyFirebaseInstanceIdService.java
public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
private static final String FRIENDLY_ENGAGE_TOPIC = "friendly_engage";
/**
* The Application's current Instance ID token is no longer valid
* and thus a new one must be requested.
*/
#Override
public void onTokenRefresh() {
// If you need to handle the generation of a token, initially or
// after a refresh this is where you should do that.
String token = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "FCM Token: " + token);
// Once a token is generated, we subscribe to topic.
FirebaseMessaging.getInstance()
.subscribeToTopic(FRIENDLY_ENGAGE_TOPIC);
}
}
Hope this will help you
I'm trying to migrate Android client app from Google Cloud Messaging to Firebase Cloud Messaging. I strictly followed an official tutorial, but didn't succeed - onMessageReceived() method is not called when app in foreground.
So here are code snippets that I touched.
build.gradle (project level)
dependencies {
//other stuff
classpath 'com.google.gms:google-services:3.0.0'
}
build.gradle (app level)
apply plugin: 'com.google.gms.google-services' //this line in the bottom
and
dependencies {
//other stuff here
compile 'com.google.firebase:firebase-messaging:9.0.0'
}
AndroidManifest.xml
<service android:name=".service.FirebaseService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
and
<service
android:name=".service.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
and
<service
android:name=".service.RegistrationIntentService"
android:exported="false" />
as childs of <application> tag.
MyInstanceIDListenerService.java
public class MyInstanceIDListenerService extends FirebaseInstanceIdService {
final String TAG = "Firebase instance id";
#Override
public void onTokenRefresh() {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
Intent intent = new Intent(RegistrationIntentServiceEvent.TOKEN);
intent.putExtra(RegistrationIntentServiceEvent.TOKEN, token);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
FirebaseService.java
public class FirebaseService extends FirebaseMessagingService {
//OtherStuff
#Override
public void onMessageReceived(RemoteMessage message){
String from = message.getFrom();
Map data = message.getData();
Log.d(TAG, "Message: " + from); //Never appears in logcat
//other stuff
}
}
RegistrationIntentService
public class RegistrationIntentService extends IntentService {
private static final String TAG = "RegIntentService";
public RegistrationIntentService() {
super(TAG);
}
#Override
protected void onHandleIntent(Intent intent) {
try {
synchronized (TAG) {
String token = FirebaseInstanceId.getInstance().getToken();
Log.i(TAG, "GCM Registration Token: " + token);//This works fine
//and shows this line - 12-19 17:59:33.295 3146-5386/ru.bpc.mobilebank.bpc I/RegIntentService: GCM Registration Token: *some_token*
sendRegistrationToServer(token);
}
} catch (Exception e) {
Log.d(TAG, "Failed to complete token refresh", e);
}
}
private void sendRegistrationToServer(String token) {
Intent intent = new Intent(RegistrationIntentServiceEvent.TOKEN);
intent.putExtra(RegistrationIntentServiceEvent.TOKEN, token);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
Please, tell if i did something wrong and why registrations seems to be done properly, but the onMessageReceived() method is never called even if the app is in foreground. Thanks in advance.
P.S. By the way, is adding SHA-1 keys in Firebase console necessary? maybe this could cause the problem? But Firebase says this action is optional.
if you are using FCM then you should replace this
<service android:name=".service.GcmService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
to
<service android:name=".service.FirebaseService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
your service not registered properly..
I want to send message from phone to wear (android watch). I dont want to send Notification. So I am using MessageApi to send the message. For some reason, my watch is not getting it. I used the same code in reverse order (wear to mobile), it works perfectly fine. From the phone end, its correctly recognizing the watch and display its name but in watch the message is not reached (onMessageReceived is not called).
Any help would be greatly appreciated.
The code in Mobile to send data:
private void resolveNode() {
Wearable.NodeApi.getConnectedNodes(mGoogleApiClient)
.setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
#Override
public void onResult(#NonNull NodeApi.GetConnectedNodesResult connectedNodes) {
for(Node connectedNode : connectedNodes.getNodes()) {
mNode = connectedNode;
Log.d(TAG,"Message Sender connected node"+mNode.getDisplayName());
sendMessage("Hello Hello");
}
}
});
}
private void sendMessage(final String message) {
if(mGoogleApiClient != null &&
mGoogleApiClient.isConnected() &&
mNode != null) {
Log.d(TAG,"Message is going to be sent to watch");
Wearable.MessageApi.sendMessage(mGoogleApiClient,
mNode.getId(),
"/mobile_data",
message.getBytes())
.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
#Override
public void onResult(#NonNull MessageApi.SendMessageResult sendMessageResult) {
if(sendMessageResult.getStatus().isSuccess()) {
Log.e(TAG,"Message Succesfully sent to watch=>"+message);
} else {
Log.e(TAG,"Message FAILED TO BE SENT to watch=>"+message);
}
}
});
}
}
Code on the wearable side
public class MessageReceiver extends WearableListenerService {
private static final String TAG = "MessageReceiver";
#Override
public void onMessageReceived(MessageEvent messageEvent) {
super.onMessageReceived(messageEvent);
Log.d(TAG,"pathpathpathpath");
}
}
Registered my Message Receiver in AndroidManifest of wearable side
<service android:name=".MessageReceiver">
<intent-filter>
<action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
<action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
<data
android:host="*"
android:pathPrefix="/mobile_data"
android:scheme="mobile" />
</intent-filter>
</service>