10

ASyncTask からのアップロードが成功したという通知を一日中取得しようとしています。現在のコードからエラーは発生していませんが、通知バー (または他の場所) に表示する通知を取得できません。LogCat にメッセージが表示されず、通知バーに通知が表示されません。これは私のコードです:

Notification mNotification = new Notification(icon, tickerText, when);

CharSequence contentTitle = "upload completed.";
CharSequence contentText = "upload completed.";

Intent notificationIntent = new Intent(context, CastrActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_NO_CREATE);
mNotification.contentIntent = contentIntent;
mNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mNotification);

これは、ASyncTask の onPostExecute() メソッドから呼び出されます。正直なところ、PendingIntent の部分については少し混乱しています。私が間違ったコードであると思われるものを明確にしていただければ幸いです。

4

5 に答える 5

31

あなたの問題は解決しましたが、通知が表示されなかったという私の問題をどのように解決したかを投稿します。おそらく、他の人が答えを読むのに役立つかもしれません。

通知ビルにアイコンがありませんでした。setSmallIcon(R.drawable.ic_launcher)通知が表示されたようなものを追加するとすぐに。

于 2013-02-06T17:43:36.460 に答える
4

通知を表示するクラスを作成しました:

public class NotificationData {

    public static NotificationManager mNotificationManager;
    public static int SIMPLE_NOTFICATION_ID;
    private Context _context;

    public NotificationData(Context context) {
        _context = context;
    }

    public void clearNotification() {
        mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);
    }

    public void SetNotification(int drawable, String msg, String action_string, Class cls) {
        mNotificationManager = (NotificationManager) _context.getSystemService(Context.NOTIFICATION_SERVICE);
        final Notification notifyDetails = new Notification(drawable, "Post Timer", System.currentTimeMillis());
        long[] vibrate = { 100, 100, 200, 300 };
        notifyDetails.vibrate = vibrate;
        notifyDetails.ledARGB = 0xff00ff00;
        notifyDetails.ledOnMS = 300;
        notifyDetails.ledOffMS = 1000;
     // notifyDetails.number=4;
        notifyDetails.defaults =Notification.DEFAULT_ALL;
        Context context = _context;
        CharSequence contentTitle = msg;
        CharSequence contentText = action_string;      
        Intent notifyIntent = new Intent(context,  cls);
     // Bundle bundle = new Bundle();
     // bundle.putBoolean(AppConfig.IS_NOTIFICATION, true);
        notifyIntent.putExtras(bundle);
        PendingIntent intent = PendingIntent.getActivity(_context, 0,notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
        notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
        mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);        
    }
}

このクラスの使用方法:

NotificationData notification; //create object
notification = new NotificationData(this);
notification.SetNotification(R.drawable.notification, "Notification Title", "Click to open", YourClassName.class);

権限を追加android.permission.VIBRATE

于 2012-01-27T07:46:57.223 に答える
2

もう1つ試してみるのは、マニフェストに次のものが含まれていることを確認することです

<permission android:name="android.permission.STATUS_BAR_SERVICE" android:protectionLevel="signature" />

また、私のものは、同じNOTIFICATION_IDを持つ連続した通知を無視しているように見えました。

于 2012-05-03T01:44:22.407 に答える
2

これを試して:

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

int icon = R.drawable.icon;        // icon from resources
CharSequence tickerText = "Any thing";              // ticker-text
long when = System.currentTimeMillis();         // notification   time
Context context21 = getApplicationContext();      // application   Context
CharSequence contentTitle = "Anything";  // expanded message title
CharSequence contentText = (CharSequence)  extras.get("message");     // expanded message text

Intent notificationIntent = new Intent(this, MainStart.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,   notificationIntent, 0);

// the next two lines initialize the Notification, using the configurations above
Notification notification = new Notification(icon, tickerText, when);
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
/*  long[] vibrate = { 0, 100, 200, 300 };
notification.vibrate = vibrate;
notification.ledARGB = Color.RED;
notification.ledOffMS = 300;
notification.ledOnMS = 300;*/
notification.setLatestEventInfo(context21, contentTitle, contentText, contentIntent);
mNotificationManager.notify(Constants.NOTIFICATION_ID, notification);
于 2012-01-27T06:18:57.600 に答える