0

Freshly downloaded XtifyCordovaSample 1.3.1 - correctly configured and receiving gcm push messages, however cordova (javascript) only receives them when they are launched from the notification center.

I would expect the example "Notification Received" to update with the push message content even when the app is open.

any hints or is this by design?

Peter Madsen
  • 419
  • 5
  • 6

2 Answers2

2

This is by design, if you want to receive notifications only when the app is open you need to the following in your activity that extends DroidGap:

package com.xtify.cordova.samples;

import android.content.Context;
import android.content.IntentFilter;
import android.os.Bundle;

import com.xtify.cordova.XtifyCordovaPlugin;
import com.xtify.sdk.api.XtifyBroadcastReceiver;

public class MainActivity extends org.apache.cordova.DroidGap {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.loadUrl("file:///android_asset/www/index.html");
    }

    @Override
    protected void onResume() {
        super.onResume();
        this.registerReceiver(this.xtifyReceiver, new IntentFilter(
                "com.xtify.sdk.NOTIFIER"));
    }

    @Override
    protected void onPause() {
        super.onPause();
        this.unregisterReceiver(this.xtifyReceiver);
    }

    private XtifyBroadcastReceiver xtifyReceiver = new XtifyBroadcastReceiver() {
        @Override
        protected void onRegistered(Context arg0) {
            // no-op
        }

        @Override
        protected void onMessage(Context arg0, Bundle msgExtras) {
            XtifyCordovaPlugin.processActivityExtras(msgExtras,
                    MainActivity.this);
        }

        @Override
        protected void onC2dmError(Context arg0, String arg1) {
            // no-op
        }
    };
}

And also you need to modify com.xtify.cordova.XtifyCordovaPlugin.processActivityExtras method to the following :

public static void processActivityExtras(Bundle msgExtras, DroidGap dg) {
    if (msgExtras != null && (msgExtras.getString("com.xtify.sdk.NOTIFICATION_TITLE") != null)) {
        try {
            JSONObject data = new JSONObject();
            for (String key : msgExtras.keySet()) {
                data.put(key, msgExtras.getString(key));
            }
            Context context = dg.getApplicationContext().getApplicationContext();
            String eventCallBackName = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getString(KEY_CALLBACK, "");
            String js = "javascript:" + eventCallBackName + "(" + data.toString() + ")";
            dg.sendJavascript(js);
        } catch (JSONException e) {
            Log.e(TAG, "Error processing activity extras", e);
        }
    }
}

Also make sure you set the notification action to NONE in the Xtify console or in the Push API.

Feras
  • 21
  • 1
  • thanks, though I want to receive push both in background and in foreground (useful for payload data - which acts as one of many fallbacks for us..). in the end we hacked it see my answer.. – Peter Madsen Sep 11 '12 at 13:54
0

this is hackish code for also receiving push notifications in foreground running app (useful for payload data..)

(working from the xtify cordova sample)

add this before the @Override public void onCreate:

private static MainActivity single;
public static MainActivity get() { return single; }

public MainActivity() {
    super();
    single = this;
}

this enables us to call:

XtifyCordovaPlugin.processActivityExtras(msgExtras, MainActivity.get());

in the XtifyNotifier.java onMessage function.. and thus the push is received in foreground and passed to cordova/javascript..

Peter Madsen
  • 419
  • 5
  • 6