在Phonegap中推送通知

时间:2015-12-03 08:02:19

标签: cordova

我使用phonegap开发了移动应用。推送通知有问题。我想要的就是当我在mysql数据库中更新新内容时,它会向所有客户端发出警报。我在git中找到了一些例子。但它不是我想要的。并且通知警报不在状态栏中。如下图所示。 This is image that i screenshot in my Phone This is image that my push notification in php using wamp 这是我在git https://github.com/khengsopheak/PushNotification

中的项目

1 个答案:

答案 0 :(得分:0)

您好,我在github中看到您的代码,您需要在GCMIntentService.java文件中进行一些更改,以便从push接收json params。

注意:请根据我的应用程序要求使用此代码作为参考

My Json Response from push

GCMIntentService.java

public void createNotification(Context context, Bundle extras)
{
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String appName = getAppName(this);

    Intent notificationIntent = new Intent(this, BHC.class);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    notificationIntent.putExtra("pushBundle", extras);
    notificationIntent.putExtra("notify", true);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    int defaults = Notification.DEFAULT_ALL;
    if (extras.getString("defaults") != null) {
        try {
            defaults = Integer.parseInt(extras.getString("defaults"));
        } catch (NumberFormatException e) {}
    }
    NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(context)
            .setDefaults(defaults)
            .setSmallIcon(context.getApplicationInfo().icon)
            .setWhen(System.currentTimeMillis())
            .setContentTitle(extras.getString("alert"))
            .setTicker(extras.getString("alert"))
            .setContentIntent(contentIntent)
            .setAutoCancel(true);

    Random random = new Random();
    int m = random.nextInt(9999 - 1000) + 1000;
    mNotificationManager.notify(m, mBuilder.build());
}

Mainactivity.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.init();
    LaunchState = "1";
    // Set by <content src="index.html" /> in config.xml
    super.setIntegerProperty("splashscreen", R.drawable.bhc_splash_screen);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

    // Set by <content src="index.html" /> in config.xml
    // super.loadUrl(Config.getStartUrl());
    super.clearCache();

    JavaScriptInterface jsInterface = new JavaScriptInterface(this);
    appView.getSettings().setJavaScriptEnabled(true);
    appView.addJavascriptInterface(jsInterface, "android");

    super.loadUrl("file:///android_asset/www/index.html");
    // appView.addJavascriptInterface(new JavaScriptInterface(), "android");
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {
        this.appView.setOverScrollMode(View.OVER_SCROLL_NEVER);
    }
    onNewIntent(getIntent());
}


protected void onNewIntent(Intent intent) {

    String strActivityId = "", strTypeName = "", strTypeId = "";
    if (intent.getExtras() != null
            && intent.getExtras().containsKey("notify")) {

        // Toast.makeText(getApplicationContext(),
        // intent.getExtras().toString(), Toast.LENGTH_LONG).show();
        Log.i("Intent", intent.getExtras().toString());
        boolean isNotify = intent.getExtras().getBoolean("notify");
        Bundle bundle;
        if (isNotify) {
            bundle = intent.getBundleExtra("pushBundle");
            // Toast.makeText(getApplicationContext(), "bundle"+
            // bundle.toString(), Toast.LENGTH_LONG).show();

            strActivityId = bundle.getString("ActivityID");
            strTypeName = bundle.getString("TypeName");
            strTypeId = bundle.getString("TypeID");

            Log.i("Alert", bundle.getString("alert"));
            Log.i("ActivityID", bundle.getString("ActivityID"));
            Log.i("TypeName", bundle.getString("TypeName"));
            Log.i("TypeID", bundle.getString("TypeID"));

            if (bundle != null) {
                if (LaunchState == "1") {
                    // appView.loadUrl("file:///android_asset/www/index.html#cat="+bundle.getString("category")+"&subcat="+bundle.getString("subcategory"));
                    // appView.loadUrl("file:///android_asset/www/index.html#activityid="+strActivityId+"&typename="+strTypeName+"&typeid="+strTypeId+"");
                    this.appView
                            .sendJavascript("javascript:pushNotificationFunction('"
                                    + strTypeId
                                    + "','"
                                    + strTypeName
                                    + "','" + strActivityId + "')");
                    LaunchState = "0";
                } else {
                    this.appView
                            .sendJavascript("javascript:pushNotificationFunction('"
                                    + strTypeId
                                    + "','"
                                    + strTypeName
                                    + "','" + strActivityId + "')");
                }
            }
        }
    } else {
        // Toast.makeText(getApplicationContext(), "else",
        // Toast.LENGTH_LONG).show();
        appView.loadUrl("file:///android_asset/www/index.html");
        LaunchState = "0";
    }

    super.onNewIntent(intent);
}