0

このコードを使用してアプリに GCM メッセージを送信して通知を作成しています

private static void generateNotification(Context context, int type, String title, String message) {
    Intent notificationIntent;
    int icon = R.drawable.ic_launcher;
    java.util.Random v = new java.util.Random();
    int id = v.nextInt(1000);
    long when = System.currentTimeMillis();
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder notification = new NotificationCompat.Builder(context);
    notificationIntent = new Intent(context, Home.class);
    notificationIntent.putExtra(CommonUtilities.TITLE_ALERT, title);
    notificationIntent.putExtra(CommonUtilities.EXTRA_MESSAGE, message);
    notificationIntent.putExtra(CommonUtilities.TYPE, type);
    notificationIntent.putExtra(CommonUtilities.ID, id);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, type, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
    Notification notification_view = notification.setContentTitle(title)
            .setContentText(message).setContentIntent(intent)
            .setSmallIcon(icon).setWhen(when)
            .setVibrate(new long[] { 1000 }).build();
    notification_view.flags |= Notification.FLAG_AUTO_CANCEL;

    // Play default notification sound
    notification_view.defaults |= Notification.DEFAULT_SOUND;

    // notification_view.sound = Uri.parse("android.resource://" +
    // context.getPackageName() + "your_sound_file_name.mp3");

    // Vibrate if vibrate is enabled
    notification_view.defaults |= Notification.DEFAULT_VIBRATE;
    manager.notify(id, notification_view);
}

レシーバーを使用してこの保留中のインテントを受信する

private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(!intent.hasExtra(CommonUtilities.TYPE)){
            Log.v("msg", "intent vars not received");
            return;
        }
        int type = intent.getExtras().getInt(CommonUtilities.TYPE);
        String title = intent.getExtras().getString(CommonUtilities.TITLE_ALERT);
        String newMessage = intent.getExtras().getString(CommonUtilities.EXTRA_MESSAGE);
        String[] msgArr = newMessage.split(",");    
        Log.v("message", newMessage);
    }
};

しかし、私のアクティビティはアクションを実行しておらず、ログを表示しています。を使用してカスタムインテントでレシーバーを登録しました

registerReceiver(mHandleMessageReceiver, new IntentFilter(CommonUtilities.DISPLAY_ACTION));

どうすればエラーを見つけることができますか?

編集

アプリケーションがフォアグラウンドにあるときに通知を受信した場合、通知は適切に受信されますが、アクティビティが実行されていないか終了していて、通知をクリックして何も起こらなかった場合

4

2 に答える 2

1

あなたが書いた:

アプリケーションがフォアグラウンドにあるときに通知を受信した場合、通知は適切に受信されますが、アクティビティが実行されていないか終了していて、通知をクリックして何も起こらなかった場合

呼び出してアクティビティから受信者を登録する場合:

registerReceiver(mHandleMessageReceiver, new IntentFilter(CommonUtilities.DISPLAY_ACTION));

次に、アクティビティのコンテキストを使用して受信者を登録しました。つまり、アクティビティが終了すると、登録されたレシーバーが削除されて破棄されます (メモリ リークを防ぐため)。

アプリが実行されていなくてもレシーバーを実行する場合は、<receiver>定義に適切なインテント フィルターを追加して、マニフェストにレシーバーを登録する必要があります。

        <intent-filter>
            <!-- use the correct name string for CommonUtilities.DISPLAY_ACTION) -->
            <action android:name="blah.blah.blah.DISPLAY_ACTION"/>
        </intent-filter>
于 2013-07-10T13:09:32.980 に答える