1

我想从服务启动前台通知。这是我的代码:

显现 :

<receiver android:name=".PowerConnectionReceiver">
        <intent-filter>
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
            <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
        </intent-filter>
</receiver>

广播接收器

@Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
            Toast.makeText(context, "Charger connected (#)", Toast.LENGTH_SHORT).show();
            Intent startIntent = new Intent(context, NotificationService.class);
            startIntent.setAction("a_abhi_apps.batterychargenotifier.action.startforeground");
            context.startService(startIntent);
        } else {
            intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED);
            Toast.makeText(context, "Charger disconnected (#)", Toast.LENGTH_SHORT).show();
stopIntent.setAction("a_abhi_apps.batterychargenotifier.action.stopforeground");
            }
        }

NotificationService.java

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        if (intent.getAction().equals("a_abhi_apps.batterychargenotifier.action.startforeground")) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_launcher_foreground)
                    .setContentTitle("Charging ")
                    .setContentText("Phone is charging");

            Intent notificationIntent = new Intent(this, MainActivity.class);
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            builder.setContentIntent(contentIntent);

            // Add as notification
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//            manager.notify(0, builder.build());
            startForeground(0, builder.build());
        } else if (intent.getAction().equals("a_abhi_apps.batterychargenotifier.action.stopforeground")) {
            stopForeground(true);
            stopSelf();
        }
        return START_STICKY;
    }

我已在清单中声明广播接收器,因为它接收到电源状态已更改。因此 MainActivity 中没有相关代码。

我在 NotificationService.java 中评论了这一行:

manager.notify(0, builder.build());

如果我删除评论,我会收到通知。但我想让它坚持通知空间,以便我可以使用该服务执行一些其他操作。

但我无法将其作为前台通知。

4

3 回答 3

0

您必须在startForeground(0, builder.build())中更改通知 ID 。您不能使用 0 作为通知 ID。

这里

此通知的标识符,根据 NotificationManager.notify(int, Notification); 不得为 0

于 2017-12-21T13:04:16.647 回答
0

尝试这个

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

    NotificationCompat.Builder note = new NotificationCompat.Builder(this)
            .setContentTitle(getString(R.string.name))
            .setSmallIcon(R.drawable.quantum_ic_refresh_white_24);

    note.setOnlyAlertOnce(true);
    note.setOngoing(true);
    note.setWhen( System.currentTimeMillis() );
    note.setAutoCancel(false);
    note.setContentText(getString(R.string.downloading_new_teachkits));

    Notification notification = note.build();
    mNotificationManager.notify(notifyID, notification );
于 2017-12-21T12:43:31.597 回答
0

您可以按照以下代码:-

 private static int notification_id=1001;

    NotificationCompat.Builder mBuilder;
    mBuilder = new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_launcher3)
                        .setContentTitle(title)
                        .setContentText(DeviceName)
                        .setSubText(timestamp)
                        .setVisibility(visibility)
                        .setPriority(NotificationCompat.PRIORITY_HIGH)
                        .setVibrate(vibrate)
                        .setOngoing(true)
                        .setFullScreenIntent(null, true);

    Intent resultIntent = new Intent(this, NotificationActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            stackBuilder.addParentStack(MainActivity.class);
            stackBuilder.addNextIntent(resultIntent);

    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);

    NotificationManagerCompat mNotificationManager = (NotificationManagerCompat) NotificationManagerCompat.from(this);
    Notification notification = mBuilder.build();


            notification.defaults |= Notification.DEFAULT_VIBRATE;
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            mNotificationManager.notify(notification_id, notification);

    startForeground (notification_id,  notification)
    notification_id = notification_id + 1;
于 2017-12-21T12:57:07.263 回答