通知()上没有出现通知

时间:2018-01-26 03:13:43

标签: android notifications notificationmanager notification-channel

我最近遇到了很多难以在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 个答案:

答案 0 :(得分:1)

它与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);

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

Documenation - NotificationChannel

Examples

答案 1 :(得分:0)

惊讶没有人跳过这一个。我找到了答案。 API级别不匹配。显然,使用高于Oreo的版本的通知需要NotificationChannel。当我在较低的API上尝试它时,它工作。知道那种导致我的手能够让它工作。如果有其他人有这个问题让我知道,也许我可以帮忙。