简体   繁体   English

我如何从推送通知服务中获取消息并显示到消息活动中

[英]how can i get the message from my push notification service and display to my message activity

(here is my notification) (这是我的通知)

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Intent intent = new Intent(this, Message.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,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.drawable.ic_event_note_black_24dp);
        notificationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0,notificationBuilder.build());
    }
}

and my message activity 和我的消息活动

public class Message extends Activity {

    private TextView  textViewTitle;
    private TextView  textViewContent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_message);
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    int width = dm.widthPixels;
    int height = dm.heightPixels;

    textViewTitle = (TextView) findViewById(R.id.textViewTitle);
    textViewContent = (TextView) findViewById(R.id.textViewTitle);

    if (getIntent().getExtras() != null) {
        //init message
        String message = String.valueOf(getIntent().getExtras().get("message"));
        String title = String.valueOf(getIntent().getExtras().get("title"));
        //save the message
        textViewTitle.save(getApplicationContext() , "message" , message);
        textViewContent.save(getApplicationContext() , "title" , title);
        startActivity(new Intent(getApplicationContext() , Message.class));
    }
}

Inside your MyFirebaseMessagingService you send a broadcast with your data. MyFirebaseMessagingService内部,您将发送包含数据的广播。

Intent intent = new Intent("myAction");
intent.putExtra("title", title);
intent.putExtra("message", message);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

And inside your activity you register a BroadcastReceiver to an IntentFilter . 在您的活动中,您将BroadcastReceiver注册到IntentFilter

public class Message extends Activity {

    private MyReceiver myReceiver;

    @Override
    protected void onStart() {
        super.onStart();
        if (myReceiver == null) {
            myReceiver = new MyReceiver();
        }
        // Take care here. The same action defined in IntentFilter
        // of your MyFirebaseMessagingService must be defined here
        LocalBroadcastManager.getInstance(this)
              .registerReceiver(myReceiver, new IntentFilter("myAction"));
    }

    @Override
    protected void onStop() {
        super.onStop();
        LocalBroadcastManager.getInstance(this)
              .unregisterReceiver(myReceiver);
    }

    private class MyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            // Here you get your data
            String title = intent.getStringExtra("title");
            String message = intent.getStringExtra("message");
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何打开一个活动以显示消息推送通知? - how to open an activity to display message push notification? 如何从服务中获取数据或消息? - How do I get the data or message from my service? 当用户点击推送通知消息时,我的应用程序启动。 如何使应用程序完整显示推送消息? - When user taps on the push notification message, my app launches. How to make the app display the push message in full? 当用户在聊天中收到另一个用户的新消息时,如何创建通知? - How can I create a notification when a user receives new message from another user in my chat? 如何让我的输入验证工作,以便 -1 - 100 范围之外的任何内容都将显示错误消息? - How can I get my input validation to work so anything outside range -1 - 100 will display the error message? 如何使用 OneSignal 从静默推送通知中获取消息? - How to get a message from a silent Push notification with OneSignal? 在我的应用程序中显示特定的通知消息时出现问题 - Issue in display particular Notification message in my application Android - 当收到推送通知 FCM 时,如何更新我的活动中的数据或 UI? - Android - How can I update data or UI in my Activity when Push Notification FCM is received? 如何解决我的小程序此消息? - How I can fix this message with my applet? 我如何在我的应用程序中使用推送通知? - how I can use push notification in my application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM