android - 如何让 MobileFirst Foundation SDK 忽略由 MobileFirst 以外的来源发送的推送通知?

标签 android ios notifications google-cloud-messaging ibm-mobilefirst

MobileFirst Foundation SDK 推送通知接收器似乎无法区分来自 MobileFirst 的通知和来自其他来源的通知。

我们正在尝试在我们的应用程序中实现推送通知,以便它可以从多个来源接收通知。但我们观察到的是,虽然其他云推送提供商的 SDK 处理来自该提供商的推送通知源,但 MobileFirst SDK 处理设备收到的所有推送通知。这会导致从云提供商发送的通知在设备上显示两次。

一些额外的细节:

  • 这似乎与第 3 方云提供商无关。我们已经尝试了 5 次并且都忽略了 MobileFirst 通知,但是 MobileFirst 处理来自所有这些通知的通知。
  • MobileFirst 的 GCM 发件人 ID 与其他提供商的相同。
  • 我们正在构建原生 Android 和 iOS 应用程序。

最佳答案

MobileFirst 不支持开箱即用的此功能,但是因为其他推送服务不使用用于通过 MobileFirst 发送通知的 MobileFirst 适配器,关键是向通知负载添加一个属性,该属性可以告诉设备是否显示通知。

因此,例如,一种解决方案是在适配器中将值为“mfp”的自定义属性添加到有效负载中,如下所示:

notification = WL.Server.createDefaultNotification(notificationText,
               badgeDigit, {custom:"mfpush"});

然后在Android原生代码中的MyListener.java中,在onReceive()函数中添加一个“if”语句。这将处理应用程序在前台运行时的情况:

public void onReceive(String props, String payload) {
  JSONObject jsonObject;
  JSONObject payloadJSON;
  String notification = "";
  String payloadNotif = "";

  try {
    // get payload from MFP adapter: custom property
    payloadJSON = new JSONObject(payload);
    payloadNotif = payloadJSON.getString("custom");

    // if the payload "custom" property is "mfp", show the alert,
    // if not, don't show the alert
    if (payloadNotif.contains("mfp")) {
      jsonObject = new JSONObject(props);
      notification = jsonObject.getString("alert");
    }else{
      return;
    }
  } catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
  MainActivity.alertMsg("Notification", notification);
}

在应用程序在后台运行的情况下,需要创建一个新类来扩展 com.worklight.wlclient.push.WLBroadcastReceiver 并覆盖 receive 方法。创建一个 CustomBroadcastReceiver 类,扩展 WLBroadCastReceiver 并覆盖 onReceive 方法以调用 CustomGCMIntentService

public class CustomBroadcastReceiver extends WLBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
     intent.setClassName(context,
CustomGCMIntentService.class.getName());
        WakefulIntentService.sendWakefulWork(context, intent);     } }

然后在 CustomGCMIntentService 中,扩展 MFP GCMIntentService 类并覆盖两个通知方法以检查来自 MFP 的推送:

package com.sample.eventsourcenotificationsandroid.custom;
import android.content.Context;
import android.content.Intent;
import com.worklight.wlclient.push.GCMIntentService;
import org.json.JSONObject;
public class CustomGCMIntentService extends GCMIntentService {
    @Override
public void notify(Context context, String tickerText) {
 super.notify(context, tickerText);
}

@Override
public void notify(Context context, String alert, int badge, String
sound, Intent intent) {
 if(isMobileFirstNotification(intent)) {
  super.notify(context, alert, badge, sound, intent);
}     }

@Override
public void notify(Context context, Message message, Intent intent) {
     if(isMobileFirstNotification(intent)) {
     super.notify(context, message, intent);
}     }

private boolean isMobileFirstNotification(Intent intent) {
 Message message = intent.getParcelableExtra("message");
 JSONObject payload = message.getPayload();
 return payload.optBoolean("mfpush", false);
} }

检查来自 MFP 的通知的方法是检查通知负载中的 mfppush 键是否为真值。

data: {
 badge: "",
 alert: "YourMessageContent",
 sound: "your sound",
 payload:{
  mfpush: true
} }

最后,需要更新 list 以使用新类而不是 com.worklight.wlclient.push.WLBroadcastReceiver,如下所示:

<service android:name="com.worklight.wlclient.push.GCMIntentService" />

<receiver android:name="com.worklight.wlclient.push.WLBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">

<!-- removed intent-filter for com.google.android.c2dm.intent.RECEIVE
-->
  <intent-filter>
    <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
    <category android:name="com.sample.eventsourcenotificationsandroid" />
  </intent-filter>
</receiver>

<!-- start custom service and receiver -->
<service android:name="com.sample.eventsourcenotificationsandroid.custom.
CustomGCMIntentService" />

<receiver     android:name="com.sample.eventsourcenotificationsandroid.custom.CustomB    roadcastReceiver"
  android:permission="com.google.android.c2dm.permission.SEND" >

  <intent-filter>
    <action android:name="com.google.android.c2dm.intent.RECEIVE" />
    <category android:name="com.sample.eventsourcenotificationsandroid" />
  </intent-filter>
</receiver>

<!-- end custom service and receiver -->

关于android - 如何让 MobileFirst Foundation SDK 忽略由 MobileFirst 以外的来源发送的推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39664312/

相关文章:

ios - UIView 中的 UIButton 在另外两个 UIView 后面对上面的 View 的触摸使用react

java - 按下音量按钮时如何使通知静音?

c# - ios webview - 如何在应用程序未运行时检查通知

android - Android 模拟器 QVGA 中的 SD 卡问题

java - 无法读取刚刚写入的文件

android - 如何为在 Android 中绘制的图形设置动画

ios - RestKit - 发布或放置包含嵌套实体的实体

android - 是否有任何内置/默认声音可以在应用程序中使用?

ios - 以 NSException 类型的未捕获异常终止,连接检查器中没有错误,线程 1 断点 1.1,多个错误

android - Galaxy S 2.3.3 中的通知文本颜色