Мои PushNotification не запускаются в личном идентификаторе канала - PullRequest
0 голосов
/ 14 июня 2019

Я разработал свое простое приложение, чтобы попробовать Intent Service. Это приложение дает от простого редактирования текста число, и когда пользователь нажимает кнопку «Запуск службы», мое приложение запускает My Service:

 public class MyService extends IntentService {
    String name="MyService";
    int i=0;


    public MyService() {
        super("MyService");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        /*per esplorare il bundle
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            for (String key : bundle.keySet()) {
                Object value = bundle.get(key);
                Log.d("KEY", String.format("%s %s (%s)", key,
                        value.toString(), value.getClass().getName()));
            }
        }*/
        String value=intent.getStringExtra("i_key");
        i=Integer.parseInt(value);
        startTimer();

    }

    @Override
    public void onStart(@Nullable Intent intent, int startId) {
        Log.d("MyService","Start");
        super.onStart(intent, startId);



    }

    private void startTimer() {
        Log.d("Start Timer",String.valueOf(i));
        final Timer timer=new Timer();
        TimerTask task=new TimerTask() {
            @Override
            public void run() {

                if(i<10) {
                    i++;
                    Log.d("Timer",String.valueOf(i));
                }
                else {
                    Intent intent=new Intent("myIntent");
                    Log.d("Fine","FineTimer");
                    intent.putExtra("value",i);
                    sendBroadcast(intent);
                    timer.cancel();
                }

            }
        };
        timer.schedule(task,0l,1000);
    }

    @Override
    public void onDestroy() {
        Log.d("MyService","Chiusura Servizio");
        super.onDestroy();
    }
}

Когда мой Ввод> 10, Сервис отправляет сообщение Broadcast Receiver, который отправляет push-уведомление:

private BroadcastReceiver myBrodcast=new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        int i=intent.getIntExtra("value",0);
        Intent myIntent=new Intent(getApplicationContext(),MainActivity.class);
        PendingIntent intent1=PendingIntent.getActivity(getApplicationContext(),0,myIntent,PendingIntent.FLAG_CANCEL_CURRENT);
        Notification notification=new NotificationCompat.Builder(getApplicationContext(),NotificationChannel.ID)
                .setSmallIcon(R.drawable.ic_not)
                .setContentTitle("My_tile")
                .setContentText(String.valueOf(i))
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                .setContentIntent(intent1)
                .build();


        notificationManagerCompat.notify(1,notification);

    }

};

MyChannelNotification создает в этом приложении:

    public class NotificationChannel extends Application {
    public static final String ID = "My_id";

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

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
            String CHANNEL_ID = "my_channel_01";
            CharSequence name = "my_channel";
            String desc = "This is my channel";
            int Importance = NotificationManager.IMPORTANCE_DEFAULT;
            android.app.NotificationChannel channel = new android.app.NotificationChannel(ID, name, Importance);
            channel.setDescription(desc);
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);

        }
    }
}

А это мой манифест:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.raffaelecoppola.service">

    <application
        android:name=".NotificationChannel"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".MyService"></service>
    </application>

</manifest>

Когда я запускаю приложение на эмуляторе Android с 26 SDK, у меня появляется эта ошибка:

Как можно решить эту проблему? Спасибо за помощь

...