Hy everyone, I am developing an app where I am using Firebase Cloud Messaging. But I have situation where I don't want to user see when notification with data messages is received. I have solved that with deleting function sendNotification from myFirebaseMessagingService, but that works only when my app is in the foreground. My question is: When app is in the background and notification comes to the system tray, how to set the code so that notification icon would not be displayed?
Here is my MainActivity:
public class MainActivity extends AppCompatActivity {
Button dugme, dugme2, dugmeBaza, dugmeToken;
DataBaseHelper db;
Cursor c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("onCreate", "ONCREATE");
db=new DataBaseHelper(this);
final Intent intent=getIntent();
setContentView(R.layout.activity_main);
String msg = getIntent().getStringExtra("click_action");
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (msg != null)
{
Log.d("MSG", msg);
if (msg.equals("goToFragment1")) {
Fragment1 fragment1 = new Fragment1();
fragmentTransaction.replace(R.id.myFragment, fragment1);
Log.d("FragmentTransaction", "Fragment je promenjen u onCreate!");
fragmentTransaction.commit();
Log.d("Create", "Kraj onCreatea");
}
}
dugme = (Button) findViewById(R.id.dugme1);
dugme2 = (Button) findViewById(R.id.subscribe);
dugme.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment fragment = null;
if (view == dugme) {
fragment = new Fragment1();
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.myFragment, fragment);
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
transaction.commit();
}
});
dugme2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FirebaseMessaging.getInstance().subscribeToTopic("android");
Log.d("Log", "Uspesno ste se pretplatili");
}
});
dugmeBaza=(Button)findViewById(R.id.dugmeZabazu);
viewAll();
dugmeToken=(Button)findViewById(R.id.TokenButton);
dugmeToken.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d("TOKEN", "Refreshed token: " + refreshedToken);
}
});
@Override
protected void onPause() {
super.onPause(); // Always call the superclass method first
Log.d("onPause", "Pauza");
}
public void viewAll(){
dugmeBaza.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Cursor res= db.getAlldata();
if(res.getCount()==0) {
//show message
showMessage("Error", "Nothing found");
return;
}
StringBuffer buffer=new StringBuffer();
while (res.moveToNext()){
buffer.append("Id: " + res.getString(0) + "\n");
buffer.append("poruka: " + res.getString(1));
}
showMessage("Data", buffer.toString());
}
});
}
public void showMessage(String title, String message){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
public void Ubaci(){
String msg=getIntent().getStringExtra("poruka");
db.insertMsg(msg);
}
And here is myFirebaseMessagingService:
public class myFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG="MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d("onMessageReceived", "Pozvana funkcija onMessageReceived");
Log.d(TAG, "From " + remoteMessage.getFrom());
Log.d(TAG, "Body " + remoteMessage.getNotification().getBody());
Log.d(TAG, "Location " + remoteMessage.getNotification().getClickAction());
Log.d(TAG, "Value " + remoteMessage.getData().get("click_action"));
This is actually controllable by the Sender of push notification. As per current documentation:
Notification: GCM automatically displays the message to end user devices on behalf of the client app. Notifications have a pre-defined set of user-visible keys. Set notification payload. May have optional data payload. Always collapsible.
Data: Client app is responsible for processing data messages. Data messages have only custom key/value pairs. Set data payload only. Can be either collapsible or non-collapsible.
To disable auto display of notifications when app is in background, make the sender not send "notification" part of payload and send everything in "data" part of the payload. That way the app code will always handle incoming message. Then it may chose to display a notification for it or not.