【问题标题】:Notifications not working with Android oneplus3 phone通知不适用于 Android oneplus3 手机
【发布时间】:2018-03-22 08:17:53
【问题描述】:

我有这个功能来生成通知。我也根据奥利奥的要求更新了代码。这里可能是什么问题。任何帮助表示赞赏。它适用于所有其他手机。我也重新检查了通知设置

private void sendNotification(String messageTitle,String messageBody) {
        android.net.Uri sound;
        Intent intent = new Intent(getActivity(), MainActivity.class);
        //intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), 0 /* request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        long[] pattern = {500, 500, 500, 500, 500};
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        SharedPreferences demoprefs = getActivity().getSharedPreferences("demo", MODE_PRIVATE);
        demiIsLoggedIn = demoprefs.getBoolean("demo", true);
        if (!demiIsLoggedIn)
            sound = defaultSoundUri;
        else
            sound = null;
        NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(getActivity())
                .setSmallIcon(getActivity().getApplicationInfo().icon)
                .setContentTitle(messageTitle)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setVibrate(pattern)
                .setLights(Color.BLUE, 1, 1)
                .setSound(sound)
                .setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String CHANNEL_ID = "my_channel_01";// The id of the channel.
            CharSequence name = "notification channel";// The user-visible name of the channel.
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
            notificationManager.createNotificationChannel(mChannel);
        }
        notificationManager.notify(12 /* ID of notification */, notificationBuilder.build());
        Log.d("wajdan","oneplus");

    }

【问题讨论】:

  • 一加上的日志有用吗?
  • 是的,我正在获取日志

标签: android oneplusthree


【解决方案1】:

尝试在notificationBuilder中设置NotificationChannel

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getActivity().getApplicationContext(), "my_channel_01")
...

【讨论】:

  • 我相信 2-arg 构造函数是 NotificationCompat.Builder 的新事物,正如这里所讨论的 stackoverflow.com/questions/45462666/…
  • 但是它显示一个错误说“Builder (android.content.context) Builder cannot be applied to (class,java.lang.string)”,我正在使用构建工具 27.0.3,什么现在改吗?
  • 我编辑答案尝试使用 getActivity().getApplicationContext()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-05
  • 1970-01-01
  • 1970-01-01
  • 2012-10-24
相关资源
最近更新 更多