android - 如何显示来自推送通知 fcm 的警报

标签 android push-notification

我在 firebase 例程中有以下代码。收到通知后,如何将通知消息或数据显示为警报?我服务器上的 php 代码成功地通过 id 将消息推送到 android 设备,并且在收到消息时会听到声音,但我想显示警报或传输到 fragment (我更喜欢警报)。

public class MyFirebaseMessagingService extends FirebaseMessagingService
{
    private static final String TAG = "MyFirebaseMessService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage)
    {
        commonfunc.myprint("#####_____MyFirebaseMessService_from 1 : " + remoteMessage.getFrom());


        if(remoteMessage.getData().size() > 0)
        {
            commonfunc.myprint("MyFirebaseMessService_getdata 2 : " + remoteMessage.getData());
            Map<String, String> data = remoteMessage.getData();
            String value1 = data.get("dtitle");
            String value2 = data.get("dbody");
            commonfunc.myprint("MyFirebaseMessService_getdata 2 : " + value1 + " " + value2);
            sendNotification(remoteMessage.getData().toString());
        }
        if (remoteMessage.getNotification() != null)
        {
            commonfunc.myprint("MyFirebaseMessService_getNot 3 body: " + remoteMessage.getNotification().getBody());
            commonfunc.myprint("MyFirebaseMessService_getNot 3 title: " + remoteMessage.getNotification().getTitle());

            sendNotification(remoteMessage.getNotification().getBody());
            sendNotification(remoteMessage.getNotification().getTitle());

            /*
            final String mMessage = remoteMessage.getNotification().getBody();
            Handler h = new Handler(Looper.getMainLooper());
            h.post(new Runnable() {
                public void run()
                {
                    AlertDialog alertDialog = new AlertDialog.Builder(MyFirebaseMessagingService.this).create();
                    alertDialog.setTitle("Message");
                    alertDialog.setMessage("Message follows: " + mMessage);
                    alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                    alertDialog.show();
                    return ;
                }
            });
            */

        }
    }

    private void sendNotification(String body)
    {
        commonfunc.myprint("MyFirebaseMessService_sendNotification 4a sound ");

        Intent intent = new Intent(this,MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

        Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notifiBuilder = new NotificationCompat.Builder(this)
                .setAutoCancel(true)
                .setContentTitle("Firebase Cloud Messaging")
                .setContentText(body)
                .setSound(notificationSound)
                .setSmallIcon(life.poa.webcastman.poa1.R.drawable.common_google_signin_btn_icon_dark)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0,notifiBuilder.build());

        commonfunc.myprint("MyFirebaseMessService_sendNotification 4b sound");
        //Toast.makeText(getApplicationContext(), "____MyFirebaseMessagingService " + body, Toast.LENGTH_SHORT).show();

    }

}

最佳答案

使用本地广播:将其放在 onMessageReceived 中:

 Intent intent = new Intent("myFunction");
                // add data
                intent.putExtra("value1", value1);
                intent.putExtra("value2", value2);
                LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

在你的 Activity/fragment 中:

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // Extract data included in the Intent
            String t = intent.getStringExtra("value1");
            String t1 = intent.getStringExtra("value2");
            //alert data here
        }
    };


       @Override
        public void onResume() {
            super.onResume();
            LocalBroadcastManager.getInstance(this.getActivity()).registerReceiver(mMessageReceiver,
                    new IntentFilter("myFunction"));
        }

        @Override
        public void onPause() {
            super.onPause();
            LocalBroadcastManager.getInstance(this.getActivity()).unregisterReceiver(mMessageReceiver);
        }

关于android - 如何显示来自推送通知 fcm 的警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41893783/

相关文章:

java - Android Studio 连接MongoDB : Gradle build finished with 1 error

android - 删除选定的 ListView 内容时出现问题

java - 如何发送 GCM 消息

iphone - 如何向推送通知警报 View 添加操作?

android - 当应用程序被终止并处于优化电池模式时未收到 FCM 消息|一加 5t |一加 6 |错误广播 Intent 回调 : result=CANCELLED

android - 以编程方式将主题应用于按钮

android - AndEngine - 如何创建一个按钮?

android - 图 TableView 库 - 未找到 GraphViewData

ios - 从 Swift 3 中的锁屏清除本地通知

ios - 重置 iOS 应用角标(Badge)