2016-11-23 4 views
0

Я пытаюсь получить сообщение с сервера в виде push-уведомления в android. Я могу ударить по серверу, но у меня есть нулевое сообщение с сервера. Я вижу уведомление в android без сообщения.Push Notification с использованием GCM

Вот мой код

@Override 
protected void onMessage(Context context, Intent intent) { 

    if(aController == null) 
     aController = (Controller) getApplicationContext(); 

    Log.i(TAG, "Received message"); 
    String message = intent.getExtras().getString("price");   
    aController.displayMessageOnScreen(context, message); 
    // notifies user 
    generateNotification(context, message);            
} 
+1

Сообщения коды приемника , – AndroidHacker

+0

Вы передали свой идентификатор устройства на сервер – Vadivel

+0

Пожалуйста, внесите полный текст – AndroidHacker

ответ

0

В случае, если вы используете GcmListenerService

public class MyGCMService extends GcmListenerService { 

    @Override 
    public void onMessageReceived(String from, Bundle data) { 
     String message = data.getString("message"); 
     if(data.containsKey("type")) 
     { 
      String type = data.getString("type"); 
      if(type.equalsIgnoreCase("qr_update")) 
      { 
       SharedPrefUtil.setSharedPref(getApplicationContext(), "qr", ""); 
      } 
     } 
     else 
     { 
      sendNotification(message); 
     } 
    } 

    private void sendNotification(String message) 
    { 
     initiateNotification(message); 
    } 

    private void initiateNotification(String message) 
    { 
     Intent intent = new Intent(this, Splash.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); 
     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentTitle(getResources().getString(R.string.app_name)) 
       //.setContentText(message) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setStyle(new NotificationCompat.BigTextStyle() 
         .bigText(message)) 
       .setContentIntent(pendingIntent); 

     NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(0 , notificationBuilder.build()); 
    } 

} 

Хотя я рекомендую вам использовать ТСМ в качестве GCM является depricated