mejkan mejkan - 5 months ago 93
Android Question

The method setLatestEventInfo(GcmMessageHandler, String, String, PendingIntent) is undefined for the type Notification

I am trying to implement push notification in my app, but it gives error on "setLatestEventInfo". What I am doing Wrong?

Here is my code

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

}

}

Answer

Use should use this.I hope this will help

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