Java 从onReceive中创建通知时出现的问题

Java 从onReceive中创建通知时出现的问题,java,android,broadcastreceiver,android-notifications,android-broadcastreceiver,Java,Android,Broadcastreceiver,Android Notifications,Android Broadcastreceiver,这是我的问题。 我有一个带计时器的AlarmManager,当计时器过期时会触发接收器。 当计时器过期,并且在接收器内触发onReceive时,我想在通知栏中创建一个通知。。然而,由于某些原因,我遇到了一些问题。 例如,单词this始终以红色下划线。 我能够用context替换其中的一些错误, 但是,在一些地方,我仍然存在问题。 我确实做了一些不正确的事情,我真的需要一些帮助 以下是我的代码,在接收方的onReceive方法中。 任何建议都将不胜感激!谢谢大家! (请注意,单击后,我

这是我的问题。

我有一个带计时器的AlarmManager,当计时器过期时会触发接收器。

当计时器过期,并且在接收器内触发onReceive时,我想在通知栏中创建一个通知。。然而,由于某些原因,我遇到了一些问题。

例如,单词
this
始终以红色下划线。
我能够用
context
替换其中的一些错误,
但是,在一些地方,我仍然存在问题。

我确实做了一些不正确的事情,我真的需要一些帮助

以下是我的代码,在接收方的onReceive方法中。
任何建议都将不胜感激!谢谢大家!

(请注意,单击后,我希望它打开位置/GPS设置屏幕)。。

使用此代码

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());
}
通知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());
}
}


希望这对您有用。

谢谢您的代码。。我试过了,但有一些问题。。(1) 对“getSystemService”的调用是红色字母。。另外(2)通知ID也是红色字母(除非我为其声明int)。。和(3)当我调用sendNotification(字符串消息)时;“消息”到底代表什么?最后,(4)在意图中,我想打开位置/GPS设置屏幕,但是当我为登陆活动输入android.provider.Settings.ACTION\u Location\u SOURCE\u设置时,我只会得到一个大的红色下划线。。你能帮我解决这4个问题吗?检查更新的代码。1.第3点-“消息”-您希望显示通知文本我使用您建议的随机部分修复了通知ID。谢谢--但是我在
getSystemService
上仍然收到红字。。有什么建议吗?我设法修复了
getSystemService
的红色字母,并能够将
这个
替换为
上下文
,作为
PendingContent.getActivity
的第一个参数。。但是仍然存在一个问题-
当传入
NotificationCompat.Builder noBuilder=new NotificationCompat.Builder(**this**)
时,此
仍为红色。。而且它不能被
上下文
替换。。。有什么建议可以解决这个问题吗?我正在尽我最大的努力去学习。SITE MOD:我真的不确定我的问题为什么会被否决。。我提供了代码块来展示我的工作,并提出了一个合理的问题。。我对android开发还是相当陌生的,我正在尽我最大的努力学习。。然而,在这个网站上,我经常感到受到严厉的评判,因为我可能无法立即理解某些事情,或者问后续问题。。很遗憾,对于像我这样一个真心想学习的人来说,结果却遭遇了逆境和解雇,而不是来自那些否决这个问题的人的额外帮助,而不是提供富有成效的帮助。。
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());
}