我可以从firebase云消息接收通知,但是如何在用户界面上显示

时间:2017-08-07 21:53:39

标签: android firebase firebase-cloud-messaging

公共类MyFirebaseMessagingService扩展FirebaseMessagingService {     private static final String TAG =“MyFirebaseMsgService”;

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.d(TAG,"FROM:"+remoteMessage.getFrom());

    //check if the message contains data
    if(remoteMessage.getData().size()>0){
        Log.d(TAG,"Message data:"+remoteMessage.getData());

        //check if the message contains notification
        if(remoteMessage.getNotification()!=null){
            Log.d(TAG,"Message body:"+remoteMessage.getNotification().getBody());
            sendNotification(remoteMessage.getNotification().getBody());

        }
    }
}

private void sendNotification(String body){

    Intent intent=new Intent(this,MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent=PendingIntent.getActivity(this,0/*request code*/,intent,PendingIntent.FLAG_ONE_SHOT);
    //set sound notification
    Uri notifictaionSound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder notifiBuilder=new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Firebase Cloud Messaging")
            .setContentText(body)
            .setAutoCancel(true)
            .setSound(notifictaionSound)
            .setContentIntent(pendingIntent);
    NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0/*ID of notification*/,notifiBuilder.build());

公共类MainActivity扩展了AppCompatActivity {

private static final String TAG = "MainActivity";
TextView text = (TextView) findViewById(R.id.textView);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent=getIntent();

    if(intent !=null){
        String message=intent.getStringExtra("message");
        text.setText(message);

    }
    Button btnShowToken=(Button) findViewById(R.id.button_show_token);
    btnShowToken.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String token= FirebaseInstanceId.getInstance().getToken();
            Log.d(TAG, "Token:"+token);
            Toast.makeText(MainActivity.this,token, Toast.LENGTH_SHORT).show();
        }

    });
}

}

1 个答案:

答案 0 :(得分:0)

所以你可以试试这个:

Intent intent=new Intent(this,MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("message",body);
    PendingIntent pendingIntent=PendingIntent.getActivity(this,0/*request code*/,intent,PendingIntent.FLAG_ONE_SHOT);
    //set sound notification
    Uri notifictaionSound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder notifiBuilder=new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Firebase Cloud Messaging")
            .setContentText(body)
            .setAutoCancel(true)
            .setSound(notifictaionSound)
            .setContentIntent(pendingIntent);
    NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0/*ID of notification*/,notifiBuilder.build());

在MainActivity onCreate()方法中执行此操作:

Strng message=getIntent().getStringExtra("message");

if(message !=null){
  //Create ui
}

所以我不知道你想要的ui,但这会从通知中得到消息,你只需要在活动中创建ui。

<强>解释

这会在您的案例正文中创建一个带有字符串extra的intent,然后将intent设置为pendingIntent of notification。 当用户点击通知时,应用程序打开并通过单击通知检查用户是否已启动,如果是,则获取您在intent中设置的额外内容并创建ui,如果不执行任何操作。