在文本视图中显示Firebase的通知

时间:2018-03-24 14:06:16

标签: android firebase push-notification

我已在我的应用中实施了firebase云消息传递。我一直在接收消息,我想在应用程序的textview中显示该消息。

以下是处理onMessageReceived的代码..

public class FcmMessagingService extends FirebaseMessagingService {
    private Map<String, String> data;
    private static final String TAG="MyFirebaseMsgService";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        data=remoteMessage.getData();
        String message=data.get("message");
        String titledata=data.get("title");
        ManualNotification(titledata , message);
    }
private void ManualNotification(String title , String messageBody){
        Intent intent = new Intent(this, MainActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("message", messageBody);
        intent.putExtras(bundle);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        Bitmap bmp = BitmapFactory.decodeResource(this.getResources(),R.drawable.splash_img);
        Notification.BigPictureStyle bigpicture = new Notification.BigPictureStyle();
        bigpicture.bigPicture(bmp);
        NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.notifaction)
                .setContentTitle(title)
                //.setContentText(messageBody)
                .setLargeIcon(bmp)
                .setContentIntent(pendingIntent)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
                .setContentText(messageBody).setLights(Color.YELLOW, 300, 300)
                .setVibrate(new long[] { 100, 250 })
                .setDefaults(Notification.DEFAULT_SOUND)
                .setAutoCancel(true);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());
    }
}

2 个答案:

答案 0 :(得分:1)

当您需要通知FcmMessagingService时,请从Activity发送广播。

Intent intent = new Intent("com.push.message.received");
intent.putExtra("message", messageBody);// Add more data as per need
sendBroadcast(intent);

Activity注册BroadcastReceiver以接收活动。

 BroadcastReceiver receiver=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // intent will holding data show the data here 
         String message=intent.getStringExtra("message");
          tvNotificationDetails.settext(message);
        }
    };

@Override
protected void onCreate(Bundle savedInstanceState) {
registerReceiver(receiver,new IntentFilter("com.push.message.received"));
}
@Override
protected void onDestroy() {
    unregisterReceiver(receiver);
    super.onDestroy();
}

onDestroy()并不是每次都要打电话所以你可能会在onStop()中取消注册收件人,并在onStart()中注册。

答案 1 :(得分:0)

清单,外观如何。 Firebase需要2个服务(实例和消息),我们正在其中使用另一个服务进行广播。所以需要添加任何东西来接收选项。