我正在编写Firebase应用程序,以便在我的应用程序的值(数字)为红色时发送通知。当应用程序在后台运行或被杀时,我尝试发送消息,但我的程序不起作用,我不知道如何调用onMessageReceived()
方法。
Station1.java
public class Station1 extends AppCompatActivity implements getData.ReturnValue {
private MyFirebaseMessagingService FBMessagingService = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//widget id
}
if (mpxInt < mpxIntMin || mpxInt > mpxIntMax) {
Mpx.setText(valeurs.getMpx());//affiche la valeur mpx dans le textView
Mpx.setTextColor(Color.parseColor("#DF0101"));//red
FBMessagingService.onMessageReceived(RemoteMessage);//?
} else {
Mpx.setText(valeurs.getMpx());
}
}
MyFirebaseMessagingService.java
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
//you can get your text message here.
String text= data.get("text");
Intent i =new Intent(this,Station1.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder=new
NotificationCompat.Builder(this);
notificationBuilder.setContentTitle("FCM NOTIFICATION");
notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notidicationManager= (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notidicationManager.notify(0,notificationBuilder.build());
}
}