-1

プッシュ通知を実装しようとしていますが、通知も受け取りました。しかし、スイッチを入れたときに通知バーに通知する必要があります。つまり、モバイルがオンになっているときに通知を受け取った場合、通知領域に通知が表示されませんでした。デバイスがシャットダウンした場合、モバイルをオンにしたときに、通知バーでその通知を取得する必要があり、別の要件もあります。つまり、通知領域で通知を削除すると、10 分後に通知領域/バーでその通知を取得する必要があります。

どうすればこれを達成できますか?

4

3 に答える 3

1

FLAG_ONE_SHOT でPendingIntentを使用できます。

private void sendNotification(String from, String message) {
        Bundle bundle = new Bundle();
        Intent intent = new Intent(this, ChatActivity.class);
        intent.putExtra("INFO", bundle);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setContentTitle(from)
                .setSound(defaultSoundUri)
                .setContentText(message)
                .setSmallIcon(R.drawable.ic_notification)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);

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

        notificationManager.notify(0, builder.build());
    }

また、マニフェスト ファイルに WAKE_LOCK パーミッションを設定することを忘れないでください。

<uses-permission android:name="android.permission.WAKE_LOCK" />
于 2016-04-07T07:16:59.287 に答える
0

起動時に何かを行うには、マニフェストに to を登録しますBroadcastReceveiverBOOT_COMPLETED

一定時間後に何かをするために使用しますAlarmManager

于 2016-04-07T07:01:09.490 に答える