如何打开新活动并通过单击Firebase通知在textview上打印通知消息

时间:2019-02-12 14:46:27

标签: java android firebase firebase-authentication firebase-notifications

我想使我的Firebase通知可点击。单击通知后,将打开一个名为HomeActivity的新活动,我想在其中以文本视图的形式显示通知消息。

我添加了通知,并且运行良好。但是,每当我单击通知时,它都会返回到mainActivity,并且不会在textview中显示该消息。我尝试了一些代码,但是没有用。

这是我的代码,

MyFirebaseInstanceService.class

                     name         Cuisine
0        Real Talent Cafe         Italian
0        Real Talent Cafe        American
0        Real Talent Cafe           Pizza
0        Real Talent Cafe   Mediterranean
0        Real Talent Cafe        European
0        Real Talent Cafe          Fusion
1                   Dogma   International
1                   Dogma   Mediterranean
1                   Dogma        Barbecue
1                   Dogma         Spanish
1                   Dogma          Fusion
2     Taberna El Callejon   Mediterranean
2     Taberna El Callejon        European
2     Taberna El Callejon         Spanish
3                   Astor   International
3                   Astor   Mediterranean
3                   Astor        European
3                   Astor          Fusion
4  La Gaditana Castellana         Spanish
4  La Gaditana Castellana         Seafood
4  La Gaditana Castellana   International
4  La Gaditana Castellana           Diner
4  La Gaditana Castellana        Wine Bar

}

HomeActivity.class

public class MyFirebaseInstanceService extends FirebaseMessagingService {


@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    shownotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());
    String click_action = remoteMessage.getNotification().getClickAction();
    Intent i = new Intent(click_action);
    Intent in = new Intent("intentKey");
            in.putExtra("key", remoteMessage);
    LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(in);

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

}

private void shownotification(String title,String body){
        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID = "com.example.theroos.simplifiedmsging02.test";

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification",NotificationManager.IMPORTANCE_DEFAULT);
            notificationChannel.setDescription("SIMPLIFIED");
            notificationChannel.setLightColor(Color.BLUE);
            notificationChannel.setVibrationPattern(new long[]{0,100,500,1000});
            notificationChannel.enableLights(true);
            //notificationChannel.createNotificationChannel(notificationChannel);

        }

        Intent activityintent = new Intent(this,HomeActivity.class);
        PendingIntent contentintent = PendingIntent.getActivity(this,0,activityintent,0);



        NotificationCompat.Builder notificationbuilder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);

        notificationbuilder.setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.ic_mode_comment_black_24dp)
                .setContentTitle(title)
                .setContentIntent(contentintent)
                .setContentText(body)
                .setColor(Color.BLUE)
                .setAutoCancel(true)
                .setContentInfo("Info");

        notificationManager.notify(new Random().nextInt(),notificationbuilder.build());




}

@Override
public void onNewToken(String token) {
    super.onNewToken(token);
    Log.d("TOKEN",token);
}

我不知道我做错了什么,请帮助我。预先谢谢你。

2 个答案:

答案 0 :(得分:0)

有一些方法可以实现这一目标。但是关于stackoverflow也有很多类似的问题。

该方法取决于您的fcm通知有效负载。请参阅此答案。您会找到所有想要的东西。

https://stackoverflow.com/a/40185654/7140884

答案 1 :(得分:0)

首先,您不应同时创建通知和发送广播消息。 试试这个:

        public class MyFirebaseInstanceService extends FirebaseMessagingService {


    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
               NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID = "com.example.theroos.simplifiedmsging02.test";

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification",NotificationManager.IMPORTANCE_DEFAULT);
            notificationChannel.setDescription("SIMPLIFIED");
            notificationChannel.setLightColor(Color.BLUE);
            notificationChannel.setVibrationPattern(new long[]{0,100,500,1000});
            notificationChannel.enableLights(true);
            //notificationChannel.createNotificationChannel(notificationChannel);

        }

        Intent activityintent = new Intent(this,HomeActivity.class);
        PendingIntent contentintent = PendingIntent.getActivity(this,0,activityintent,0);

        NotificationCompat.Builder notificationbuilder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);

        notificationbuilder.setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.ic_mode_comment_black_24dp)
                .setContentTitle(title)
                .setContentIntent(contentintent)
                .setContentText(body)
                .setColor(Color.BLUE)
                .setAutoCancel(true)
                .setContentInfo("Info");

        notificationManager.notify(new Random().nextInt(),notificationbuilder.build());

    }
....

然后可以为广播接收器创建PendingIntent.getActivity而不是PendingIntent.getBroadcast(YourBroadcastReceiver..),您应该在清单中进行注册:

<receiver
    android:name="com.app.YourBroadcastReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="YourReceiverIntentFilter" />
    </intent-filter>
</receiver>

YourBroadcastReceiver中,您应该获得待定的意图,然后在活动中将广播消息发送到本地接收者。

希望有帮助!

相关问题