大家好,我正在开发一个使用 Firebase Cloud Messaging 的应用程序。但是我有一种情况,我不想让用户看到何时收到带有数据消息的通知。我已经通过从 myFirebaseMessagingService 中删除函数 sendNotification 解决了这个问题,但这仅在我的应用程序处于前台时才有效。我的问题是:当应用程序在后台并且通知来到系统托盘时,如何设置代码使通知图标不会显示?
这是我的主要活动:
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"));