【问题标题】:Not able get custom data, FCM notification无法获取自定义数据、FCM 通知
【发布时间】:2020-11-24 16:12:16
【问题描述】:

我一直在尝试通过深度链接实现 fcm 通知。我已经关注了这篇文章。 This Post 我使用 Cloud Messaging Dashboard 发送通知,并将自定义数据作为“deepLink”键,它是值,但我无法检索任何 remoteData ,我尝试对其进行 log.d ,但没有出现任何内容。因此,每当我发送通知时,它都会出现但会导致主要活动,而不是所需的深层链接活动。请帮忙。

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.
    }

   
}

我想发送给用户的活动清单(/处理动态链接的地方)。

<activity android:name="c.testnotification.app.MainActivity" android:screenOrientation="portrait"
        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data
                android:host="https://somedomainxyz.com/?id="
                android:scheme="https"/>
            <data
                android:host="https://somedomainxyz.com/?id="
                android:scheme="http"/>
            

        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data
                android:host="somedomainxyz.com"
                android:path="/?id="
                android:scheme="app" />
        </intent-filter>



    </activity>

这就是我通过仪表板添加自定义数据的方式

我尝试通过其他方式发送,例如完整的深层链接 URL,而不是短链接、http、https 等。但仍然没有成功。

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

【问题讨论】:

  • 请发布您发送的通知示例
  • 请显示您的清单文件,其中您定义了要将用户发送到的活动。或者如果它不是太大,就发布整个清单
  • @Kubicko 我附上了截图。请检查。
  • @SarahKhan 我已在清单中添加了该类的 sn-p。请检查。
  • 好的,那么在MainActivity中如何提取主机呢?您介意在您的 MainActivity 中发布代码吗?您在哪里接收通过深度链接发送的 url?或者您可以只发布整个 MainActivity 代码吗?

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


【解决方案1】:

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

我刚刚了解到 onMessageReceived 仅在应用程序处于前台时调用,而当应用程序处于后台时,数据作为额外的意图传递。 所以在主activity中,当app在后台时,你可以自定义key值如下:

getIntent().getExtras().getString("key");

我将查询参数作为自定义数据的值传递,而不是动态链接,然后像处理深度链接参数一样处理它。 此外,上面提到的 NotificationBuilder 声明已被弃用,因此,当应用程序处于前台时,通知不会显示。改成这个。

NotificationCompat.Builder builder = new  NotificationCompat.Builder(this, channelId)

现在要在应用程序处于前台时管理通知数据,将远程消息作为额外的意图通过pendingintent 传递给主活动,并像应用程序处于后台时一样处理它。替换

        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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-26
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    相关资源
    最近更新 更多