Android FCM 通知问题 [英] Android FCM notification issue

查看:32
本文介绍了Android FCM 通知问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Android 上遇到了 FCM 通知问题.只有当应用程序关闭且未运行时才会出现问题.我正在尝试实现无点击活动,我不想打开应用程序,也不想让消息消失.当应用程序打开并运行时,它可以完美运行.当应用程序未打开时,我仍然收到通知,但它不是多行的,所以我只能看到通知的开头.然后单击通知使其消失.我花了几个小时试图找到一个有效的解决方案.这是我到目前为止的代码:

I am struggling with FCM notifications on Android. The issue is only when the app is closed and not running. I am trying to achieve no click activity I don't want the app to open and I do not want the message to disappear. It works perfectly when the app is open and running. When the app is not open I still get the notification but it is not in multi-line so I can only see the beginning of the notification. Then clicking on the notification makes it disappear. I have spent hours trying to find a solution that works. Here is the code i have so far:

private void sendNotification(String messageBody) {

    //Intent intent = new Intent(this, MainActivity.class);
  //  intent.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
    //PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
         //   PendingIntent.FLAG_UPDATE_CURRENT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("My Car Wash App")
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentText(messageBody)
            .setDefaults(NotificationCompat.DEFAULT_ALL)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
            .setPriority(NotificationCompat.PRIORITY_MAX);

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

任何帮助或建议将不胜感激.

Any help or suggestions would be greatly appreciated.

编辑*******

问题是经过更多研究后我问的问题很糟糕我现在意识到上面的代码只有在我的应用程序打开时才会被调用.

The problem is I asked the question poorly after more research I now realize the code above is only being called when my app is open.

@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        //Displaying data in log
        //It is optional
        Log.i("myStuff", "From: " + remoteMessage.getFrom());
        Log.i("myStuff", "Notification Message Body: " +                         remoteMessage.getNotification().getBody());
        //Calling method to generate notification
        sendNotification(remoteMessage.getNotification().getBody());
} 

onMessageReceived 在应用程序不在前台时不会被调用,这就是通知不显示多行的原因,除非应用程序正在运行.有很多关于这个主题的信息,但我似乎仍然无法弄清楚.

onMessageReceived is not called when the app is not in the foreground that is why the notifications are not showing multi-line unless the app is running. There is a lot of information posted on this subject but I still can not seem to figure it out.

这是我的 c# 服务器端代码*

Here is my c# server side code *

var data = new
                    {
                        to = deviceId,
                        notification = new
                        {
                            body = msg,
                            title = "My Car Wash",
                            icon = "myicon"                   
                        },
                       priority = 10
                   };

当应用运行时,通知在 iOS 上完美运行,在 Android 上完美运行.我只是无法将消息正确显示在多行中,我不知道该怎么做.

Notifications work perfect on iOS and perfect on Android when the app is running. I just can not get the message to show in multi-line correctly and I am not sure what to do.

*

推荐答案

当应用程序关闭时,FCM 以它想要的方式显示我的通知.我没问题.我只是想确保整个通知被阅读而不是丢失.这是我的解决方案...当应用用户点击通知时,它会打开应用并在警报对话框中显示通知.

FCM displays my notification the way it wants to when the app is closed. That's ok with me. I just want to make sure the entire notification gets read and not lost. Here is my solution... When an app user clicks on the notification it opens the app and displays the notification in an alert Dialog.

发送消息的c#服务器端代码

c# server-side code to send the message

 var msg2send = new
            {
                to = deviceId,
                priority = 10,
                notification = new
                {
                    body = msg,
                    title = "Red Hill Car Wash",
                    icon = "myicon"
                },
                data = new 
                {
                    notice = msg
                }
            };

并且在MainActivity的onCreate中

And in the onCreate of MainActivity

String notice = "";
try {
         notice = getIntent().getStringExtra("notice");
        if (notice != null)
        {
            Log.i("myStuff", notice);
            ///// DISPLAY NOTIFICATION IN AN ALERT DIAGLOG all good!
        }
    }
    catch (Exception ex) {
        Log.i("myStuff", ex.getMessage());
    }

FCM 将消息传递给我的 MainActivity,如果消息存在,那么我会抓取它并可以随心所欲地显示它.感谢您的意见和意见,以帮助解决此问题.

FCM passes the message to my MainActivity, if a message exists then I grab it and can display it how ever I want. Thank for the comments and input to help solve this issue.

这篇关于Android FCM 通知问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆