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

标签 java android push-notification

我正在尝试在我的应用程序中实现推送通知,但它在“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/

相关文章:

java - dex2jar 和 jd-GUI 的替代品?

android - 当路径不存在时,Firebase childEventListener 返回值

python - 使用 Python 的服务器-客户端推送通知

java - Firebase 推送 - 身份验证凭据无效

java - 如何拆分仅包含定界符的字符串?

java - java swing中的定位

java - 找到第一个外括号

java - WearableRecyclerView Android Wear OS

javascript - HTML5 网络应用程序中的推送通知/消息

java - 查询行为改变对设计的影响——OOP范式