我创建了一个用于使用alarmmanager测试通知的应用程序。它适用于不使用通知通道的设备。
我正在尝试为android 8.0添加一个频道,但是我似乎没有得到它。
有人可以帮忙解决这个问题,以便我可以在装有Android 8.0+的设备上收到通知
ActivityMain
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final Context context = this;
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setReminder(context,ReminderReceiver.class,10);
Snackbar.make(view, "Hope", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
public void setReminder(Context context, Class<?> cls, int sec)
{
Intent intent = new Intent(context, cls);
intent.putExtra("TIME",sec);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, sec, intent,
PendingIntent.FLAG_ONE_SHOT);/* Find more about flags: https://developer.android.com/reference/android/app/PendingIntent */
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.set( AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (sec * 1000), pendingIntent );//Add time in milliseconds. if you want to minute or hour mutiply by 60.. For ex: You want to trigger 5 Min then here you need to change 5 * 60 * 1000
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
ReminderReciever
public class ReminderReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int Request_Code = intent.getExtras().getInt("TIME",0);
showNotification(context, MainActivity.class,
"New Notification Alert..!", "scheduled for " + Request_Code + " seconds",Request_Code);
}
public void showNotification(Context context, Class<?> cls, String title, String content,int RequestCode)
{
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent notificationIntent = new Intent(context, cls);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(cls);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(
RequestCode,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"Default");
Notification notification = builder.setContentTitle(title)
.setContentText(content).setAutoCancel(true)
.setSound(alarmSound).setSmallIcon(R.mipmap.ic_launcher_round)
.setContentIntent(pendingIntent).build();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "id1";
CharSequence channelName = "id1";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
assert notificationManager != null;
notificationManager.createNotificationChannel(notificationChannel);
}
//assert notificationManager != null;
assert notificationManager != null;
notificationManager.notify(RequestCode,notification);
}
}
清单
<?xml version="1.0" encoding="utf-8"?>
<application
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"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:enabled="true" android:name=".ReminderReceiver"/>
</application>
答案 0 :(得分:1)
您需要在通知生成器中添加setChannel(notificationChannel),如下所示:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"Default");
Notification notification = builder.setContentTitle(title)
.setContentText(content).setAutoCancel(true)
.setSound(alarmSound).setSmallIcon(R.mipmap.ic_launcher_round)
.setContentIntent(pendingIntent)
.setChannel(channelId).build();
在创建通知频道后添加此代码。