gpt4 book ai didi

java - 方法 setLatestEventInfo(GcmMessageHandler, String, String, PendingIntent) 未定义类型通知

转载 作者:行者123 更新时间:2023-12-01 09:48:07 24 4
gpt4 key购买 nike

我正在尝试在我的应用程序中实现推送通知,但它在“setLatestEventInfo”上给出错误。我做错了什么?

这是我的代码GcmMessageHandler.java:

public class GcmMessageHandler extends IntentService {

GoogleCloudMessaging gcm;
String regid;



NotificationManager nm;
static final int UniqueID=2154;

String mes,message;
private Handler handler;
public GcmMessageHandler() {
super("GcmMessageHandler");
}

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
handler = new Handler();
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();

GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);

mes = extras.getString("title");
message = extras.getString("message");
showToast();
Log.i("GCM", "Received : (" +messageType+") "+extras.getString("title"));



GcmBroadcastReceiver.completeWakefulIntent(intent);
nm=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);


}
@SuppressWarnings("deprecation")
public void showToast(){
handler.post(new Runnable() {
public void run() {

Intent intent=new Intent(GcmMessageHandler.this, MainActivity.class);
PendingIntent pi=PendingIntent.getActivity(GcmMessageHandler.this, 0, intent, 0);
String sms=message;
String title="Message";
//Toast.makeText(getApplicationContext(), sms, Toast.LENGTH_LONG).show();

Notification n= new Notification(R.drawable.ic_launcher,sms,System.currentTimeMillis());

//here it gives error on "n.setLatestEventInfo"
//error is"The method setLatestEventInfo(GcmMessageHandler, String, String, PendingIntent) is undefined for the type Notification"
n.setLatestEventInfo(GcmMessageHandler.this, title, sms, pi);


n.defaults=Notification.DEFAULT_ALL;
nm.notify(UniqueID, n);




Toast.makeText(getApplicationContext(),message, Toast.LENGTH_LONG).show();
}
});

}

}

最佳答案

使用应该使用这个。我希望这会有所帮助

 NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(GcmMessageHandler.this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message))

.setSound(alarmSound)

.setVibrate(vibrate)


.setAutoCancel(true)
.setContentText(message);



mBuilder.setContentIntent(alarmIntent);


Notification notification = new Notification();
notification. defaults |= Notification.DEFAULT_VIBRATE;


mNotificationManager.notify(UniqueID, mBuilder.build());


Toast.makeText(getApplicationContext(),message, Toast.LENGTH_LONG).show();

关于java - 方法 setLatestEventInfo(GcmMessageHandler, String, String, PendingIntent) 未定义类型通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37820861/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com