从服务启动时不显示带有自定义文本和图标的通知

问题描述 投票:0回答:1

所以,我有一个MainActivity,它具有一个用于启动服务MyService的按钮。我希望服务显示带有自定义标题,文本,图标等的通知。该服务是前台服务。不是绑定服务。一切正常,但是关于我的通知的唯一事情是继续显示“点击以获取更多信息”,然后单击它将我带到应用程序的设置页面。尽管在我的MainActivity上有未决的意图。

我想要的是一个带有自定义标题,文本,图标的通知,以及一个停止服务的操作。

这是我的MainActivity.java类

public class MainActivity extends AppCompatActivity {




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.start_tracking).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                startCommand();

            }
        });


    }

    void startCommand() {


        startService(new Intent(this, MyService.class));


    }

}

Myservice.java文件

public class MyService extends Service {


    boolean mServiceIsStarted = false;


    void moveToStartedState() {

        Intent myIntentbuilder = new IntentBuilder(this).setmCommandId(Command.START).build();

        Log.d(TAG, "moveToStartedState: Running on Android O - startForegroundService(intent)");
        ContextCompat.startForegroundService(this, myIntentbuilder);



    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand: ");

        boolean containsCommand = IntentBuilder.containsCommand(intent);


        routeIntentToCommand(intent);
        return START_NOT_STICKY;
    }

    void routeIntentToCommand(Intent intent) {

        if (intent != null) {

            if (IntentBuilder.containsCommand(intent)) {
                processCommand(IntentBuilder.getCommand(intent));
            } else
                commandStart();

        }


    }






    @Override
    public void onCreate() {
        super.onCreate();


        Log.d(TAG, "onCreate: ");


    }

    void showNotification() {
        Log.d(TAG, "showNotification: ");
        HandleNotification.O.createNotification(this);
    }


    private void processCommand(int command) {
        try {
            switch (command) {
                case Command.START:
                    commandStart();
                    break;
                case Command.STOP:
                    commandStop();
                    break;
            }
        } catch (Exception e) {
            e(TAG, "processCommand: exception", e);
        }
    }


    void commandStop() {
        stopSelf();
        stopForeground(true);

    }

    void commandStart() {
        if (!mServiceIsStarted) {
            mServiceIsStarted = true;
            moveToStartedState();
            return;
        }

//        
        HandleNotification.O.createNotification(this);


    }



    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


}

IntentBuilder.java类

@IntDef({Command.INVALID, Command.STOP, Command.START})
@Retention(RetentionPolicy.SOURCE)
@interface Command {

    int INVALID = -1;
    int STOP = 0;
    int START = 1;
}

public class IntentBuilder {

    private static final String KEY_MESSAGE = "msg";
    private static final String KEY_COMMAND = "cmd";
    private Context mContext;
    private String mMessage;
    private @Command
    int mCommandId = Command.INVALID;


    public static IntentBuilder getInstance(Context mContext) {
        return new IntentBuilder(mContext);
    }


    public IntentBuilder(Context mContext) {
        this.mContext = mContext;
    }

    public void setmContext(Context mContext) {
        this.mContext = mContext;
    }

    public IntentBuilder setmMessage(String mMessage) {
        this.mMessage = mMessage;
        return this;
    }

    public IntentBuilder setmCommandId(int mCommandId) {
        this.mCommandId = mCommandId;
        return this;
    }

    private static final String TAG = "IntentBuilder";

    public Intent build() {

        Log.e(TAG, "build: context cannot be null" + mContext);
        Intent intent = new Intent(mContext, MyService.class);
        if (mMessage != null)
            intent.putExtra(KEY_MESSAGE, mMessage);

        if (mCommandId != Command.INVALID)
            intent.putExtra(KEY_COMMAND, mCommandId);

        return intent;

    }

    public static boolean containsCommand(Intent intent) {
        return intent.getExtras() != null && intent.getExtras().containsKey(KEY_COMMAND);
    }

    public static boolean containsMessage(Intent intent) {
        return intent.getExtras() != null && intent.getExtras().containsKey(KEY_MESSAGE);
    }

    public static @Command
    int getCommand(Intent intent) {
        final @Command int commandId = intent.getExtras().getInt(KEY_COMMAND);
        return commandId;
    }

    public static String getMessage(Intent intent) {
        return intent.getExtras().getString(KEY_MESSAGE);
    }
}

HandleNotification.java文件

public class HandleNotification {


    public static class O {


        public static int getRandomNumber() {
            return new Random().nextInt(100000);
        }

        static PendingIntent getLaunchActivityPI(Service context) {

            Intent intent = new Intent(context, MainActivity.class);
            return PendingIntent.getActivity(context, getRandomNumber(), intent, 0);
        }

        static PendingIntent getStopServicePI(Service context) {
            PendingIntent pendingIntent;
            {
                Intent intent = new IntentBuilder(context).setmCommandId(Command.STOP).build();
                pendingIntent = PendingIntent.getService(context, getRandomNumber(), intent, 0);
            }
            return pendingIntent;
        }

        public static final Integer ONGOING_NOTIFICATION_ID = getRandomNumber();
        public static final String CHANNEL_ID = String.valueOf(getRandomNumber());

        private static final String TAG = "O";

        public static void createNotification(Service context) {
            Log.d(TAG, "createNotification: ");
            String channelId = createChannel(context);
            Notification notification = buildNotification(channelId, context);

            context.startForeground(ONGOING_NOTIFICATION_ID, notification);

        }


        static Notification buildNotification(String channelId, Service context) {

            PendingIntent piMainActivity = getLaunchActivityPI(context);
            PendingIntent piStopService = getStopServicePI(context);
            Icon poweroff = Icon.createWithResource(context, android.R.drawable.star_big_on);
            Notification.Action stopAction = new Notification.Action
                    .Builder(poweroff, "STOP", piStopService).build();

            return new Notification.Builder(context, channelId)
                    .addAction(stopAction)
                    .setContentTitle("Tracking...")

                    .setContentText("I'm tracking your location now... ")
                    .setContentIntent(piMainActivity)
                    .build();

        }


        @NonNull
        private static String createChannel(Context service) {

            NotificationManager notificationManager = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);
            CharSequence channelName = "Start Location Updates";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, channelName, importance);
            notificationManager.createNotificationChannel(notificationChannel);
            return CHANNEL_ID;

        }

    }


}
java android android-service foreground-service android-service-binding
1个回答
0
投票

使用NotificationCompat.Builder。您可以遵循如何创建一个here

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle(textTitle)
    .setContentText(textContent)
    .setPriority(NotificationCompat.PRIORITY_DEFAULT);

您可以添加自定义的大小图标,标题,内容和其他属性。

我很久以前从project中获得的示例

private void LockNotification() {
    NotificationCompat.Builder builder = new 
    NotificationCompat.Builder(getApplicationContext());

    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("key","launch_about");
    PendingIntent pendingIntent = 
    PendingIntent.getActivity(getApplicationContext(), 0, intent, 
    PendingIntent.FLAG_UPDATE_CURRENT);

    // Set the title, text, and icon
    builder.setContentTitle(getString(R.string.app_name))
            .setContentText("App Lock Enabled")
            .setSmallIcon(R.drawable.ic_applock)
            .setContentIntent(pendingIntent)
            .setOngoing(true);

    // Get an instance of the Notification Manager
    NotificationManager notifyManager = (NotificationManager)
            getSystemService(Context.NOTIFICATION_SERVICE);

    // Build the notification and post it
    notifyManager.notify(0, builder.build());
}
© www.soinside.com 2019 - 2024. All rights reserved.