0
package com.bamart.mybhaskarmart.activity;

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.NotificationCompat;
import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import mypackage.Bhaskar.mybhaskarmart.R;

/**
 * Created by Anil on 12/24/2016.
 */
public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";

    public MyFirebaseMessagingService() {
    super();
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        //Displaying data in log
        //It is optional
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());

        //Calling method to generate notification
        sendNotification(remoteMessage.getNotification().getBody());
    }

    //This method is only generating push notification
    //It is same as we did in earlier posts
    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.swadeshmarticon)
                .setContentTitle("Firebase Push Notification")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0, notificationBuilder.build());
    }


    @Override
    protected Intent zzae(Intent intent) {
        return null;
    }
}

and why zzae ovveriden method is there i am getting call back when i fire message from firebase caonsole This is my Firebase code when i run this code i am unable to get message i dont know why message is not receiving while every thing i am doing correct please suggest me.

Maklee Lee
  • 263
  • 5
  • 14

1 Answers1

0

Edit PendingIntent.FLAG_ONE_SHOT with PendingIntent.FLAG_UPDATE_CURRENT

Try this:-

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "FCM Service";
    NotificationManager notificationManager;
    Notification notification;
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(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());


        sendNotification(remoteMessage.getNotification().getBody(),remoteMessage.getNotification().getTitle(),remoteMessage.getSentTime());
    }

    private void sendNotification(String msg,String title,long time) {
        Intent notifyintent=new Intent(getApplicationContext(),MainActivity.class);
        //int id=  intent.getExtras().getInt("notificationId");
        int id=  0;

        PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(),0,notifyintent,PendingIntent.FLAG_UPDATE_CURRENT);
        notificationManager =(NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        notification= new NotificationCompat.Builder(getApplicationContext())
                .setContentTitle(title)
                .setContentText(msg)
                .setWhen(time)
                .setDefaults(Notification.DEFAULT_SOUND)
                .setAutoCancel(true)
                .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)

               .setContentIntent(pendingIntent).build();
        notificationManager.notify(id,notification);
    }
}
Nainal
  • 1,728
  • 14
  • 27
  • why am getting call back on zzae not in onMessage – Maklee Lee Dec 24 '16 at 08:38
  • and also not getting Notification in background state @Nainal – Maklee Lee Dec 24 '16 at 08:40
  • we can only send notification when app is in foreground from Firebase base console. For sending notification in background you need to do a post call. Refere the link https://firebase.google.com/docs/cloud-messaging/android/receive http://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase – Nainal Dec 24 '16 at 08:48
  • why you using the Intent overridden method, check after removing it – Nainal Dec 24 '16 at 08:50
  • because it show Error if i not override it and telling make abstacrct – Maklee Lee Dec 24 '16 at 08:51
  • I dont know why this Error is coming – Maklee Lee Dec 24 '16 at 08:52
  • i am using compile 'com.google.api-client:google-api-client:1.20.0' compile 'com.google.firebase:firebase-core:9.2.0' compile 'com.google.firebase:firebase-messaging:9.2.0' – Maklee Lee Dec 24 '16 at 08:53
  • use this compile 'com.google.firebase:firebase-core:10.0.1' compile 'com.google.firebase:firebase-messaging:10.0.1' compile 'com.google.android.gms:play-services-auth:10.0.1' – Nainal Dec 24 '16 at 09:40
  • Have u used this line at end of build.gradel(app) apply plugin: 'com.google.gms.google-services' – Nainal Dec 24 '16 at 09:41