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

时间:2016-06-14 19:48:45

标签: 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();
        }
     });

}

}

1 个答案:

答案 0 :(得分:1)

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

 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();