Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法获取自定义数据,FCM通知_Android_Firebase_Firebase Cloud Messaging_Android Notifications - Fatal编程技术网

Android 无法获取自定义数据,FCM通知

Android 无法获取自定义数据,FCM通知,android,firebase,firebase-cloud-messaging,android-notifications,Android,Firebase,Firebase Cloud Messaging,Android Notifications,我一直在尝试通过深度链接实现fcm通知。我一直关注这个帖子。 我使用云消息仪表板发送通知,自定义数据作为“deepLink”键及其值,但我无法检索任何远程数据,我尝试记录它,但没有显示任何内容。因此,每当我发送通知时,它都会出现,但会导致主要活动,而不是所需的深度链接活动。请帮忙 MyFirebaseMessagingService.java public class MyFirebaseMessagingService extends FirebaseMessagingService {

我一直在尝试通过深度链接实现fcm通知。我一直关注这个帖子。 我使用云消息仪表板发送通知,自定义数据作为“deepLink”键及其值,但我无法检索任何远程数据,我尝试记录它,但没有显示任何内容。因此,每当我发送通知时,它都会出现,但会导致主要活动,而不是所需的深度链接活动。请帮忙

MyFirebaseMessagingService.java

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage)
        {

            Map<String, String> data = remoteMessage.getData();
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());

            String title = data.get("title");
            String message = data.get("message");
            String deepLink = data.get("deepLink");
          
            Log.d(TAG,deepLink);


            sendNotification(this, title, message, deepLink);
        }

        public static void sendNotification(Context context, String title, String message, String deepLink) {

            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

            if (Build.VERSION.SDK_INT >= 26) {
                NotificationChannel notificationChannel = new NotificationChannel("xyz01", "xyz",
                        NotificationManager.IMPORTANCE_HIGH);


                    notificationManager.createNotificationChannel(notificationChannel);

            }

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

            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setSmallIcon(R.drawable.ic_stat_notifylogo)
                    .setPriority(android.app.Notification.PRIORITY_MAX)
                    .setDefaults(android.app.Notification.DEFAULT_ALL)
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_stat_notifylogo));

            Intent intent = new Intent();

            intent.setAction(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(deepLink));
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);

            notificationBuilder
                    .setContentTitle(title)
                    .setContentText(message)
                    .setContentIntent(pendingIntent);


                notificationManager.notify(0, notificationBuilder.build());

        }

    
    @Override
    public void onNewToken(String token) {
 
        sendRegistrationToServer(token);
    }
   
 
    private void handleNow() {
        //Log.d(TAG, "Short lived task is done.");
    }

    private void sendRegistrationToServer(String token) {
        // TODO: Implement this method to send token to your app server.
    }

   
}
公共类MyFirebaseMessagingService扩展了FirebaseMessagingService{
私有静态最终字符串TAG=“MyFirebaseMsgService”;
@凌驾
收到消息时公共无效(RemoteMessage RemoteMessage)
{
Map data=remoteMessage.getData();
Log.d(标记,“消息数据有效负载:”+remoteMessage.getData());
字符串title=data.get(“title”);
字符串消息=data.get(“消息”);
字符串deepLink=data.get(“deepLink”);
日志d(标签,deepLink);
sendNotification(此、标题、消息、深度链接);
}
公共静态void sendNotification(上下文上下文、字符串标题、字符串消息、字符串深度链接){
NotificationManager NotificationManager=(NotificationManager)context.getSystemService(context.NOTIFICATION\u服务);
如果(Build.VERSION.SDK_INT>=26){
NotificationChannel NotificationChannel=新的NotificationChannel(“xyz01”、“xyz”,
通知经理(重要性高);
notificationManager.createNotificationChannel(notificationChannel);
}
Uri defaultSoundUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_通知);
NotificationCompat.Builder notificationBuilder=新建NotificationCompat.Builder(上下文)
.setAutoCancel(真)
.setSound(defaultSoundUri)
.setSmallIcon(R.drawable.ic_stat_notifylogo)
.setPriority(android.app.Notification.PRIORITY_MAX)
.setDefaults(android.app.Notification.DEFAULT\u ALL)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_stat_notifylogo));
意图=新意图();
intent.setAction(intent.ACTION\u视图);
setData(Uri.parse(deepLink));
intent.addFlags(intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
PendingEvent PendingEvent=PendingEvent.getActivity(上下文,0,意图,PendingEvent.FLAG_ONE_SHOT);
通知生成器
.setContentTitle(标题)
.setContentText(消息)
.setContentIntent(挂起内容);
notificationManager.notify(0,notificationBuilder.build());
}
@凌驾
公共void onNewToken(字符串标记){
sendRegistrationToServer(令牌);
}
私有无效handleNow(){
//Log.d(标记“短期任务完成”);
}
私有void sendRegistrationToServer(字符串令牌){
//TODO:实现此方法以将令牌发送到应用程序服务器。
}
}
要将用户发送到的活动清单(/处理动态链接的位置)

我试着通过其他方式发送,比如完整的deeplinkurl,而不是short、http、https等,但仍然不起作用

这种方法也足以管理这两种情况,即当应用程序位于前台和后台时

一点上下文:我使用两个deeplink(为了简化,删除了另一个intent过滤器),id用于类A,而qid用于类B活动。在对用户进行身份验证后,根据密钥(即id、qid(对于其他类型))的不同,主活动将重定向到相应的活动,其中查询参数显示来自数据库的关联数据

我刚刚了解到,onMessageReceived仅在应用程序位于前台时调用,而当应用程序位于后台时,数据作为额外的意图传递。 因此,在主活动中,当应用程序位于后台时,您可以按如下方式获取自定义键值:

getIntent().getExtras().getString("key");
我将查询参数作为自定义数据的值传递,然后像处理deeplink参数一样处理它,而不是动态链接。 此外,上面提到的NotificationBuilder声明已被弃用,因此,当应用程序位于前台时,通知未显示。换成这个

NotificationCompat.Builder builder = new  NotificationCompat.Builder(this, channelId)
现在,要在应用程序处于前台时管理通知数据,请通过PendingEvent将remotemessage作为一个额外的意图传递给主活动,并像应用程序处于后台时一样进行处理。替换

        Intent intent = new Intent();

        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(deepLink));
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
用这个

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

请发布您正在发送的通知示例。请显示您的清单文件,其中定义了要将用户发送到的活动。或者只要把整个舱单都贴出来就行了big@Kubicko我已经附上了截图。请检查。@SarahKhan我已经在清单中添加了该类的片段。请检查。好的,那么如何在MainActivity中提取主机?你介意发布代码吗?在你的主要活动中,你在哪里收到通过deeplink发送的url?或者你可以发布整个main活动代码吗?
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
intent.putExtra("remoteMessage", remoteMessage);