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

Posted

技术标签:

【中文标题】如何通过单击 firebase 通知打开新活动并在 textview 上打印通知消息【英文标题】:How open a new activity & print the notification message on a textview by clicking on firebase notification 【发布时间】:2019-07-06 06:04:10 【问题描述】:

我想让我的 Firebase 通知可点击。单击通知后,将打开一个名为 HomeActivity 的新活动,我希望将通知消息显示为文本视图。

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

这是我的代码,

MyFirebaseInstanceService.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);

HomeActivity.class

public class HomeActivity extends AppCompatActivity 

private Button signout_button;
private TextView message_textView;
FirebaseAuth mAuth;

private BroadcastReceiver activityReceiver = new BroadcastReceiver() 
    @Override
    public void onReceive(Context context, Intent intent) 
        TextView message_textview = (TextView) findViewById(R.id.message_textView);
        String message=intent.getStringExtra("message");
        message_textView.setText(message);
    

;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    signout_button = (Button) findViewById(R.id.signoutbutton);
    //message_textView = (TextView)findViewById(R.id.message_textView);

    //message_textView.setText(getIntent().getExtras().getString("body"));

    if (activityReceiver != null) 
        IntentFilter intentFilter = new  IntentFilter("ACTION_STRING_ACTIVITY");
        registerReceiver(activityReceiver, intentFilter);
    

    LocalBroadcastManager.getInstance(HomeActivity.this).registerReceiver(
            activityReceiver, new IntentFilter("intentKey"));

    signout_button.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            FirebaseAuth.getInstance().signOut();
            startActivity(new Intent(HomeActivity.this, MainActivity.class));
            Toast.makeText(HomeActivity.this, "Successfully Signed Out", Toast.LENGTH_SHORT).show();
        
    );


   

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

【问题讨论】:

【参考方案1】:

有一些方法可以实现这一目标。但是***上有很多类似的问题。

该方法取决于您的 fcm 通知负载。请看这个答案。你会找到你要找的所有东西。

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

【讨论】:

【参考方案2】:

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

        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.getBroadcast(YourBroadcastReceiver..) 而不是 PendingIntent.getActivity,您应该在清单中注册它:

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

YourBroadcastReceiver 中,您应该获得待处理的意图,然后在您的活动中向您的本地接收器发送广播消息。

希望对你有帮助!

【讨论】:

以上是关于如何通过单击 firebase 通知打开新活动并在 textview 上打印通知消息的主要内容,如果未能解决你的问题,请参考以下文章

在应用关闭时单击 Firebase 通知后打开特定的活动/片段

单击firebase的通知时,在设备的浏览器中打开URL而不直接到App

单击 Listview 中的项目时,如何在新活动中从 Firebase 检索数据?

如何禁用或启用onMessageReceived的firebase推送通知?

单击通知不会在后台打开活动

单击通知后如何进行特定活动