みなさん、私は Firebase Cloud Messaging を使用してアプリを開発しています。しかし、データメッセージを含む通知が受信されたときにユーザーに見せたくない状況があります。関数 sendNotification を myFirebaseMessagingService から削除することで解決しましたが、それはアプリがフォアグラウンドにある場合にのみ機能します。私の質問は: アプリがバックグラウンドにあり、通知がシステム トレイに来るとき、通知アイコンが表示されないようにコードを設定するにはどうすればよいですか?
ここに私の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);
}
そして、ここに 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"));