【问题标题】:Showing alert dialog on notification click when sent from Firebase Push Notification从 Firebase 推送通知发送通知时显示警报对话框
【发布时间】:2018-01-26 08:17:27
【问题描述】:

我在我的项目中使用 firebase 推送通知。但每当我点击通知时,else 部分总是会显示。我想在点击通知时在该对话框中显示通知文本。

这是我在NotifService.class中的代码:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "FCM Service";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        NotificationCompat.Builder mBuilder =   new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.icon_main)
                .setContentTitle("Update from Refer Ncert")
                .setContentText(""+remoteMessage.getNotification().getBody())
                .setAutoCancel(true)
                .setLights(Notification.DEFAULT_LIGHTS,1000,20000)
                .setVibrate(new long[] { 1000, 1000})
                .setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("notifmsg",remoteMessage.getNotification().getBody());
        PendingIntent pi = PendingIntent.getActivity(this,0,intent,Intent.FLAG_ACTIVITY_NEW_TASK);
        mBuilder.setContentIntent(pi);
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(0, mBuilder.build());
    }
}

这是 MainActivity 的 onCreate() 中显示通知的代码:

String notifmsg=getIntent().getStringExtra("notifmsg");

    if(notifmsg != null){
        new SweetAlertDialog(this)
                .setTitleText("Hello Readers!")
                .setContentText(notifmsg)
                .show();
    }
    else{
        new SweetAlertDialog(this)
                .setTitleText("Welcome Back")
                .setContentText("Continue reading your previous book by clicking on RECENT BOOK button below.")
                .show();
    }

【问题讨论】:

    标签: android firebase notifications firebase-cloud-messaging firebase-notifications


    【解决方案1】:

    据我所知,您定义了意图,但您没有“发送”它。

    在定义 pendingIntent 后将其添加到代码中,它应该可以工作:

    pi.send();
    

    【讨论】:

    【解决方案2】:

    你可以用这个:

    Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_ONE_SHOT);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle("Notification")
                .setContentText(messageBody)
                .setContentIntent(pendingIntent);
        NotificationManager notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());
    

    【讨论】:

    • 不,我们不能从活动外部弹出警报对话框,但您可以使用广播接收器,它会触发警报对话框。收到通知后,将广播发送到 mainactivity,然后广播将收到消息。通过使用此消息,您可以弹出警报对话框
    猜你喜欢
    • 1970-01-01
    • 2022-07-16
    • 1970-01-01
    • 2022-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-18
    相关资源
    最近更新 更多