0

I am using firebase for sending data messages from rest client but i am not receiving any notifications.I also tried sending notification messages from firebase console but no response whether app is foreground or background state. I tried almost all solutions on stackoverflow regarding FCM but nothing seems to work.

Any help will be appreciated!

Here is my build.gradle

    dependencies {
    compile 'com.google.firebase:firebase-core:10.0.1'
    compile 'com.google.firebase:firebase-ads:10.0.1'
    compile 'com.google.firebase:firebase-messaging:10.0.1'
    compile 'com.google.firebase:firebase-invites:10.0.1'
}
apply plugin: 'com.google.gms.google-services'

build.gradle(project level)

  dependencies {
    classpath 'com.android.tools.build:gradle:2.2.3'
    classpath 'com.google.gms:google-services:3.0.0'
}

MyFirebaseMessagingService.java

public class MyFirebaseMessagingService extends FirebaseMessagingService {
Bitmap bitmap;

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    String message = remoteMessage.getData().get("message");
    //imageUri will contain URL of the image to be displayed with Notification
    String imageUri = remoteMessage.getData().get("image");
    String link = remoteMessage.getData().get("link");
    String market = remoteMessage.getData().get("market");
    String external = remoteMessage.getData().get("external");
    Log.d("error", link + "    " + market + "    " + message + "   " + imageUri);

    //To get a Bitmap image from the URL received
    bitmap = getBitmapfromUrl(imageUri);
    sendNotification(message, bitmap, link, market, external);

}


/**
 * Create and show a simple notification containing the received FCM message.
 */

private void sendNotification(String messageBody, Bitmap image, String link, String market, String external) {
    Intent intent = new Intent(this, NavigationActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("LINK", link);
    intent.putExtra("MARKET", market);
    intent.putExtra("EXTERNAL", external);
    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)
            .setLargeIcon(image)/*Notification icon image*/
            .setSmallIcon(R.drawable.appiconmobile)
            .setContentTitle(messageBody)
            .setStyle(new NotificationCompat.BigPictureStyle()
                    .bigPicture(image))/*Notification with Image*/
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

public Bitmap getBitmapfromUrl(String imageUrl) {
    try {
        URL url = new URL(imageUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap bitmap = BitmapFactory.decodeStream(input);
        return bitmap;

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;

    }
}

}

MyFirebaseInstanceIdService.java

public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";

@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();

     sendRegistrationToServer(refreshedToken);
}


private void sendRegistrationToServer(String token) {
    // TODO: Implement this method to send token to your app server.
}

}

Here is json data that i am sending through Advanced Rest client

{ "data": {
    "image": "https://ibin.co/2t1lLdpfS06F.png",
    "message": "Firebase Push Message Using API"
    "link": "http://www.google.com"
  },
  "to" : "/topics/shopbrowser"
}

MainActivity.java

.....
    subscribeToPushService();
....


  private void subscribeToPushService() {
    FirebaseMessaging.getInstance().subscribeToTopic("shopbrowser");


    String token = FirebaseInstanceId.getInstance().getToken();

}
Shubham Shukla
  • 988
  • 2
  • 13
  • 28
  • Are you positive that the device is subscirbed to the `shopbrowser` topic? Can't see any `subscribeTo()` calls in the code snippet you provided. – AL. Jan 14 '17 at 08:31
  • @AL. i have posted the code. – Shubham Shukla Jan 14 '17 at 08:45
  • The code looks okay. One thing that's bugging me though is the `getBitmapfromUrl`. Is the logs in `onMessageReceived` not showing? – AL. Jan 14 '17 at 23:51
  • @AL. i think it was not working because i have enabled offline mode in project. this was written before in log window D/FirebaseInstanceId: background sync failed: INVALID_PARAMETERS – Shubham Shukla Jan 14 '17 at 23:57
  • Hey. Sorry, what do you mean by *enabled offline mode in project*? Can you post the complete Logs? – AL. Jan 15 '17 at 03:27
  • http://stackoverflow.com/a/32173484/6291914 now when i disabled this mode it worked.i don't know what was the issue – Shubham Shukla Jan 15 '17 at 05:07

0 Answers0