通知未出现在notify()上

7

最近我在使用Android Studio时遇到了许多困难,特别是一些具体的功能无法正常工作。最近,我正在尝试显示简单的通知,但它们从未出现过。我感觉我已经尝试了所有的方法。以下是我的代码。

public class NotificationReceiver extends BroadcastReceiver {

private final String TAG = NotificationReceiver.class.getSimpleName();

@Override
public void onReceive(Context context, Intent intent) {
    Intent notificationIntent = new Intent(context, ScrollingActivity.class);

    TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
    taskStackBuilder.addParentStack(ScrollingActivity.class);
    taskStackBuilder.addNextIntent(notificationIntent);

    PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(100, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "channel");

    Notification notification = builder.setContentTitle("My Test Notification")
            .setContentText("This is some sample test notification. (Congratulations!)")
            .setTicker("New Message Alert!")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pendingIntent).build();

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notification);
}}

这是我使用的调用通知的方法。
 public void setRepeatingNotification(){
    Calendar repeatCalendar = Calendar.getInstance();

    repeatCalendar.set(Calendar.HOUR_OF_DAY, 21);
    repeatCalendar.set(Calendar.MINUTE, 40);
    repeatCalendar.set(Calendar.SECOND, 10);

    Intent intent = new Intent(ScrollingActivity.this, NotificationReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(ScrollingActivity.this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, repeatCalendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

    Toast.makeText(ScrollingActivity.this, "Repeating Notification Set", Toast.LENGTH_LONG).show();
}

这里是清单文件。
<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
    android:name=".MainApplication"
    android:allowBackup="true"
    android:icon="@mipmap/skillset_v3"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    >
    <activity
        android:name=".ScrollingActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".TaskViewActivity"
        android:label="@string/title_activity_task_view"
        android:parentActivityName=".ScrollingActivity"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.jermaineoliver.aswitch.ScrollingActivity" />
    </activity>
    <activity
        android:name=".MasteryViewActivity"
        android:label="@string/title_activity_mastery_view"
        android:parentActivityName=".ScrollingActivity"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.jermaineoliver.aswitch.ScrollingActivity" />
    </activity>
    <service
        android:name="TaskTrackingService"/>
    <receiver android:name=".NotificationReceiver"/>
</application>

请帮帮我!我已经在做这个问题很久了。

2个回答

9

这与NotificationChannel有关。在Android Oreo及以上版本中,您可以像这样创建一个NotificationChannel:

NotificationManager notificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

String channelId = "my_channel_id";
CharSequence channelName = "My Channel";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel = new NotificationChannel(channelId,             channelName, importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{1000, 2000});
notificationManager.createNotificationChannel(notificationChannel);

然后您可以按照以下步骤创建通知:
NotificationManager notificationManager = 
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int notifyId = 1;
String channelId = "my_channel_id";

Notification notification = new Notification.Builder(MainActivity.this)
    .setContentTitle("My Message")
    .setContentText("My test message!")
    .setSmallIcon(R.drawable.ic_notification)
    .setChannel(channelId)
    .build();

notificationManager.notify(id, notification);

这样,通知将使用正确的通知渠道并能正确显示。您还可以为通知渠道创建组。在此处阅读更多信息:

文档 - NotificationChannel

示例


1
谢谢。只有在将setChannel替换为setChannelId后,上述代码才能正常工作。 - Ahmed Gad

0

我很惊讶没有人抓住这个问题。我找到了答案。API级别不匹配。显然,使用高于Oreo版本的通知需要NotificationChannel。当我在较低的API上尝试时,它可以工作。知道这一点就让我得以解决问题。如果其他人有这个问题,请告诉我,也许我可以提供帮助。


1
这就是我之前回答的内容 ;) - Fatmajk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接