无法从服务启动前台通知?

时间:2017-12-21 12:32:28

标签: android service notifications foreground-service

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

清单:

<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());

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

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

3 个答案:

答案 0 :(得分: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 );

答案 1 :(得分:0)

您可以按照以下代码: -

 <a href="#" id="register"><i class="fa fa-plus-square"></i> Register</a>
    <div class="ct-popupForm ct-js-productForm" id ="Registration">
        <div class="container">
          <form role="form" class="center-block" method="POST">
                <div class="form-group">
                    <div class="ct-form-content">
                        <div class="row">
                            <div class="col-md-6">
                                <label>First Name</label>
                                <input type="text" required class="form-control input-lg" value="Kristine" placeholder="">
                                <label>Last Name</label>
                                <input type="text" required class="form-control input-lg" value="Black" placeholder="">
                                <label>Number of Properties</label>
                                <input type="text" required class="form-control input-lg" value="15" placeholder="">
                                <label>Include contact form?</label>
                            </div>
                         </div>
                        <button type="submit" class="btn btn-success center-block">Save changes</button>
                    </div>
                    <div class="ct-form-close"><i class="fa fa-times"></i></div>
                </div>
            </form>
        </div>
    </div>
    <script>
    $(function(){
    var options = {
            "backdrop" : "static",
            "show":true
        }
    $("#register").on("click", function(){
        console.log('hi');

       $('#Registration').modal(options);
    });
});
</script>

答案 2 :(得分:0)

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

来自here

  

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