状态栏通知操作/回调

时间:2011-05-01 15:44:59

标签: android cordova

我正在试图弄清楚如何在通知选项卡时添加处理程序/操作,这是我到目前为止的代码。

package com.phonegap.plugins.SystemNotification;

import org.json.JSONArray;
import org.json.JSONException;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import org.json.JSONArray;

public class SystemNotification extends Plugin {

    final int notif_ID = 1234;
    NotificationManager notificationManager;
    Notification note;
    PendingIntent contentIntent;

    @Override
    public PluginResult execute(String action, JSONArray args, String callbackId)
    {

        PluginResult.Status status = PluginResult.Status.OK;
        String result = "";

        try {
            if (action.equals("createStatusBarNotification")) {
                this.createStatusBarNotification(args.getString(0), args.getString(1), args.getString(2));
            }
            else if (action.equals("updateNotification")) {
                this.updateNotification(args.getString(0), args.getString(1), args.getInt(2));
            }
            else if (action.equals("cancelNotification")) {
                this.cancelNotification();
            }
            else if (action.equals("showTickerText")) {
                this.showTickerText(args.getString(0));
            }
            return new PluginResult(status, result);
        } catch(JSONException e) {
            return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
        }
    }

    private void updateNotification(String contentTitle, String contentText, int number)
    {
        note.setLatestEventInfo(this.ctx, contentTitle, contentText, contentIntent);
        note.number = number;
        notificationManager.notify(notif_ID,note);
    }

    private void createStatusBarNotification(String contentTitle, String contentText, String tickerText)
    {
        notificationManager = (NotificationManager) this.ctx.getSystemService(Context.NOTIFICATION_SERVICE);
        note = new Notification(android.R.drawable.btn_star_big_on, tickerText, System.currentTimeMillis() );
    //change the icon

    Intent notificationIntent = new Intent(this.ctx, SystemNotification.class);
        notificationIntent.setAction(Intent.ACTION_MAIN);
        notificationIntent = notificationIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        contentIntent = PendingIntent.getActivity(this.ctx, 0, notificationIntent, 0);

        note.defaults |= Notification.DEFAULT_SOUND;
        note.defaults |= Notification.DEFAULT_VIBRATE;
        note.defaults |= Notification.DEFAULT_LIGHTS;
        note.defaults |= Notification.FLAG_AUTO_CANCEL;

        note.ledARGB = 0xff00ff00;
        note.ledOnMS = 300;
        note.ledOffMS = 1000;
        note.flags |= Notification.FLAG_SHOW_LIGHTS;

        note.setLatestEventInfo(this.ctx, contentTitle, contentText, contentIntent);

        note.number = 1;  //Just created notification so number=1. Remove this line if you dont want numbers

        notificationManager.notify(notif_ID,note);
    }

    private void cancelNotification()
    {
        notificationManager.cancel(notif_ID);
    }

    private void showTickerText(String tickerText)
    {
        note.tickerText = tickerText;
        notificationManager.notify(notif_ID,note);
    }

    @Override
    public void onPause()
    {
        super.webView.loadUrl("javascript:window.plugins.SystemNotification.onBackground();");
    }

    @Override
    public void onResume()
    {
        super.webView.loadUrl("javascript:window.plugins.SystemNotification.onForeground();");
    }

}

代码来自https://github.com/saileshmittal/phonegap-system-notification-plugin 我究竟做错了什么?当我点击通知时,现在什么也没发生。

编辑// androidmanifest:

    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".HelloAndroid"
              android:label="@string/app_name"
              android:configChanges="orientation|keyboardHidden"
              android:launchMode="singleTop">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

1 个答案:

答案 0 :(得分:1)

因为我正在使用phonegap我需要在Intent中更改类。在我的情况下,这是HelloAndroid。那就是在Droidgap上扩展的那个。

Intent notificationIntent = new Intent(this.ctx,SystemNotification.class);

Intent notificationIntent = new Intent(this.ctx,HelloAndroid.class);

感谢您的帮助!