我遇到以下代码的问题,它应该显示和响铃通知。通知没有显示。
@Override
protected void onHandleIntent(Intent intent) {
Bitmap remote_picture = null;
long when = System.currentTimeMillis();
int icon = getNotificationIcon();
Bundle gcmData = intent.getExtras();
Random random = new Random();
int count = random.nextInt(9999 - 1000) + 1000;
if (intent.getExtras().getString("price") != null && intent.getExtras().getString("img") != null) {
NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle();
notiStyle.setSummaryText(intent.getExtras().getString("price"));
try {
remote_picture = BitmapFactory.decodeStream((InputStream) new URL(intent.getExtras().getString("img")).getContent());
} catch (IOException e) {
e.printStackTrace();
}
notiStyle.bigPicture(remote_picture);
NotificationManager notificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = null;
Intent gotoIntent = new Intent();
gotoIntent.setClassName(this, "com.vspf.LoginCheckActivity");
contentIntent = PendingIntent.getActivity(this,
(int) (Math.random() * 100), gotoIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.logo1);
Notification notification = mBuilder.setLargeIcon(bitmap)
.setTicker("VSPF")
.setWhen(0)
.setAutoCancel(true)
.setContentTitle("VSPF")
.setStyle(new NotificationCompat.BigTextStyle().bigText(intent.getExtras().getString("price")))
.setContentIntent(contentIntent)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentText(intent.getExtras().getString("price"))
.setStyle(notiStyle)
.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
count++;
notificationManager.notify(count, notification);
}
}
private int getNotificationIcon() {
boolean whiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
return whiteIcon ? R.drawable.logo1 : R.drawable.logo1;
}
答案 0 :(得分:0)
尝试修改代码,如下例所示......
Notification.Builder builder = new Notification.Builder(this.getApplicationContext());
builder.setTicker("VSPF")
builder.setWhen(0)
builder.setAutoCancel(true)
builder.setContentTitle("VSPF")
builder.setContentIntent(contentIntent);
builder.setContentText(intent.getExtras().getString("price"))
builder.setStyle(notiStyle)
......
......
Notification notification = builder.build();
notificationManager.notify(count, notification);