要在 Android 上显示的自定义广播接收器块通知 - IBM mobilefirst

Custom broadcast receiver block notification to display on Android - IBM mobilefirst

我想检测通知何时到达应用程序。根据文档,我们不能使用原生 android API 来通知 Hyrbide 应用程序。 我遵循了本指南 检测通知何时到达然后将数据发送到 javascript 部分:https://www.ibm.com/support/knowledgecenter/SSZH4A_6.2.0/com.ibm.worklight.dev.doc/devref/c_push_notifications_hybrid.html

问题是广播接收器很好地接收了通知,但没有显示。这是广播接收器的代码:

 @Override
protected void onStart() {
    super.onStart();       
    String action = getPackageName() + "." + getString(R.string.app_name) + ".C2DM_MESSAGE";
    //Register Custom Push Broadcast Receiver
    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {               
                Log.d("test", "action :" + intent.getAction());
                Log.d("test", "notification received)
                abortBroadcast();       
        }
    }, new IntentFilter(action));
}

我通过不使用 registerReceiver 而是使用自定义 class 解决了这个问题:

public class MyNotificationReceiver  extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if(action.equals("com.google.android.c2dm.intent.RECEIVE")){
        JSONObject data = new JSONObject();
        try {
            data.put("notifReceived",  true);
            Log.d("test", "is the notif background ? ");
            WL.getInstance().sendActionToJS("androidNotif", data);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

并在您的清单中添加:

<receiver android:name=".MyNotificationReceiver" android:enabled="true">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
            <category android:name="com.your.appname"/>
        </intent-filter>
</receiver>