我的PushNotification不在个人频道ID中运行

时间:2019-06-14 12:20:28

标签: android push-notification notification-channel

我已经开发了简单应用程序来尝试Intent 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时,服务会向广播接收方发送一条消息,后者会发送推送通知:

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>

当我使用26个SDK在Android模拟器上启动我的应用程序时,出现此错误:

如何解决此问题? 感谢您的帮助

0 个答案:

没有答案