2015-04-04 56 views
1

這是我第一次嘗試使用Android推送通知。我有推動工作,並收到通知,但收到的消息是一個刺痛。通過GCM通知,但只需顯示消息不是整個字符串

捆綁[{機器人= { 「警報」: 「這是一個消息」},來自= 512815927951,android.support.content.wakelockid = 1,collapse_key的= do_not_collapse}]

所有我想看到通知提醒。「這是一個消息」

這裏是我的GCMIntentService.java

import android.app.IntentService; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.SystemClock; 
import android.provider.Settings; 
import android.support.v4.app.NotificationCompat; 
import android.util.Log; 

import com.google.android.gms.gcm.GoogleCloudMessaging; 

public class GcmIntentService extends IntentService { 
    public static final int NOTIFICATION_ID = 1; 
    private static final String TAG = "GcmIntentService"; 
    private NotificationManager mNotificationManager; 
    NotificationCompat.Builder builder; 

    public GcmIntentService() { 
     super("GcmIntentService"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     Bundle extras = intent.getExtras(); 
     String msg = intent.getStringExtra("message"); 
     GoogleCloudMessaging gcm; 
     gcm = GoogleCloudMessaging.getInstance(this); 


     String messageType = gcm.getMessageType(intent); 

     if (!extras.isEmpty()) { 

      if (GoogleCloudMessaging. 
        MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { 
       sendNotification("Send error: " + extras.toString()); 
      } else if (GoogleCloudMessaging. 
        MESSAGE_TYPE_DELETED.equals(messageType)) { 
       sendNotification("Deleted messages on server: " + 
         extras.toString()); 
      } else if (GoogleCloudMessaging. 
        MESSAGE_TYPE_MESSAGE.equals(messageType)) { 
       Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime()); 

       sendNotification(extras.toString()); 

       Log.i(TAG, "Received: " + extras.toString()); 
      } 

     } 
     // Release the wake lock provided by the WakefulBroadcastReceiver. 
     GcmBroadcastReceiver.completeWakefulIntent(intent); 
    } 

    // Put the message into a notification and post it. 
    // This is just one simple example of what you might choose to do with 
    // a GCM message. 
    private void sendNotification(String msg) { 
     mNotificationManager = (NotificationManager) 
       this.getSystemService(Context.NOTIFICATION_SERVICE); 

     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
       new Intent(this, MainActivity.class), 0); 



     NotificationCompat.Builder mBuilder = 
       new NotificationCompat.Builder(this) 
         .setSmallIcon(R.drawable.ic_stat_gcm) 
         .setContentTitle("GCM Notification") 
         .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) 
         .setContentText(msg); 

     mBuilder.setVibrate(new long[] { 1000, 1000}); 
     mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI); 
     mBuilder.setContentIntent(contentIntent); 
     mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
    } 



} 

回答

0

根據您的輸出,您發送以下名稱/值對 - android={"alert":"This is a Message"}

這意味着您應該獲得「android」鍵的值,並將其解析出來。

變化

sendNotification(extras.toString()); 

String android = extras.getString("android"); 
int begin = android.indexOf(":\""); 
int end = android.indexOf("\"}"); 
String text = null; 
if (begin >= 0 && end >= begin) { 
    text = android.substring(begin+2,end); 
} 
sendNotification(text); 
+0

感謝您的答覆!我改變了這個,現在沒有在通知中的文字? – 2015-04-04 17:11:29

+0

@SamKing我誤解了你的extras.toString()輸出。查看編輯 – Eran 2015-04-04 17:24:37

+0

工作正常!但是我在消息文本的末尾看到了一個}?非常感謝你.. – 2015-04-04 17:33:17

0

更改此

String msg = intent.getStringExtra("message"); 

這個

String msg = extras.getString("alert"); 
+0

Android工作室表示msg是一個未使用的變量? – 2015-04-04 17:10:53

+0

將'sendNotification(extras.getString(「alert」)'改爲'sendNotification(msg)' – Aadi 2015-04-04 17:24:40

+0

Eran的建議得到了它的工作,但是謝謝你的回覆和幫助! – 2015-04-04 17:53:04