这是我的问题..
我有一个带有定时器的AlarmManager,它会在定时器到期时触发接收器。
当Timer到期,并且在接收器中触发onReceive时,我想在通知栏中创建 NOTIFICATION 。但是,出于某种原因我遇到了问题..
例如,单词this
总是用红色加下划线。
我能够用context
替换其中的一些,以摆脱一些错误,
但是有几个地方我仍然有问题..
我肯定做错了一些事情,并且可以真正使用一些帮助!
以下是我的代码,在接收者的onReceive方法中。
非常感谢任何建议!谢谢!
(PS ..点击后,我希望它打开位置/ GPS设置屏幕)..
// Sets an ID for the notification
int mNotificationId = 001;
NotificationManager mNotifyMgr =
(NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification_messageboxes)
.setContentTitle("TITLE OF NOTIFICATION")
.setContentText("Description text of the notification");
Intent resultIntent =
new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
PendingIntent resultPendingIntent =
PendingIntent.getActivity
(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
答案 0 :(得分:2)
使用此代码
private void sendNotification(String message) {
Intent intent = new Intent(this, "YOUR CLASS WHERE LAND".class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
int requestCode = 0;
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("TITLE")
.setContentText(message)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setSound(sound);
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, noBuilder.build());
}
NOTIFICATION_ID每次都是唯一ID。当您在Receiver的onReceive()方法中收到某些内容时调用此方法。
更新代码:以下是完整代码:
public class GCMPushReceiverService extends GcmListenerService {
Random random = new Random();
int NOTIFICATION_ID = random.nextInt(9999 - 1000) + 1234;
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
Log.e("Notification ", "Received onMessage : " + data);
sendNotification(message);
}
private void sendNotification(String message) {
Intent intent = new Intent(this, "YOUR CLASS WHERE LAND".class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
int requestCode = 0;
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title")
.setContentText(message)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setSound(sound);
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, noBuilder.build());
}
}
希望这对你有用。