我在android.it中使用谷歌云消息显示警报正在某些设备和模拟器中工作,但不能正常工作,如Samsunng Galaxy Grand和我的一些模拟器,它只显示通知的图标和标题消息是空白的。
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MyActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Siom Alert")//TITLE CAN BE TSKEN FROM HERE
.setStyle(new NotificationCompat.BigTextStyle().bigText(mes))
.setContentText(mes);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
答案 0 :(得分:1)
请尝试这个。
// Put the message into a notification and post it.
// MainActivity is the activity which you wish to load. Setting setWhen() with 0 will not publish time.
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.your_app_icon)
.setContentTitle(getResources().getText(R.string.your_title))
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setWhen(0).setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentText(msg)
.setAutoCancel(true);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
我希望你拥有所需的权限。 参考:GCM Reference
答案 1 :(得分:0)
尝试这样可以帮到你
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MyActivity.class), 0);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Siom Alert")//TITLE CAN BE TSKEN FROM HERE
.setStyle(new NotificationCompat.BigTextStyle().bigText(mes))
.setContentText(mes);
} else {
//code for simple notification
}
答案 2 :(得分:0)
试试这段代码。
private void generateNotification(Context context, String message) {
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, message, when);
String title = "New POC Alert";
Intent notificationIntent = new Intent(context, null);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
// Mainfest权限
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="vibrate" />