我在前台的服务没有显示自定义通知android

时间:2016-07-13 10:15:03

标签: android service foreground-service

我提供了服务,我试图在前台启动它。它的工作原理但问题是它只显示标准通知而不是我的自定义通知。显示的通知是标准消息" name_of_application正在运行如果您想要更多信息,请点击此处"

我启动该服务的活动代码是:

    void StartServiceNavigation(){


            Intent startIntent = new Intent(PlanTheTripActivity.this, FollowUserService.class);
            startIntent.setAction(Constants.ACTION.STARTFOREGROUND_ACTION);
            startService(startIntent);

我的服务代码是:

        public class FollowUserService extends Service {
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

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

            if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {
                Toast.makeText(this,"Start Service in ForeGround",Toast.LENGTH_SHORT).show();
                Log.i("service", "Received Start Foreground Intent ");


                Intent notificationIntent = new Intent(this, PlanTheTripActivity.class);
                notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
                notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationInten`enter code here`t, 0);

                RemoteViews notificationView = new RemoteViews(getPackageName(),R.layout.notification);
                notificationView.setTextViewText(R.id.testo_custom,"provaprova");

                 Intent previousIntent = new Intent(this, FollowUserService.class);
                 PendingIntent ppreviousIntent = PendingIntent.getService(this, 0, previousIntent, 0);


                 Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.muoversi_a_roma);

                Notification notificationCustom = new NotificationCompat.Builder(this)
                        .setContentTitle("Navigazione Muovi Roma")
                        .setTicker("AOOOOOO")
                        .setContentText("CHE FINE HA FATTO")
                        .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
                        .setContent(notificationView)
                        .setContentIntent(pendingIntent)
                        .addAction(R.drawable.ic_media_play, "Play", ppreviousIntent)
                        .setOngoing(true)
                        .build();



                startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,
                        notificationCustom);
            }





            return super.onStartCommand(intent, flags, startId);
        }

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

我的自定义布局的代码是:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/notification_image"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:src="@drawable/muoversi_a_roma"/>

    <TextView
        android:id="@+id/testo_custom"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:text="prova"
        />

0 个答案:

没有答案
相关问题