2012-11-25 94 views
0

我從here運行谷歌GCM測試代碼和它運行在仿真器和電話,但是當我做了發送消息通知只能在模擬器上,而不是手機。Notifcation沒有顯示在手機上,但確定在模擬器

GCMIntentService Notifcation

import android.support.v4.app.NotificationCompat; 
... 

// issues a notification to inform the user that server has sent a message 
private void generateNotification(Context context, String message) { 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_launcher) 
      .setTicker(context.getString(R.string.app_name)).setContentText(message); 

    Intent notificationIntent = new Intent(context, Main.class); 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    builder.setContentIntent(contentIntent); 

    // add notification 
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    manager.notify(FM_NOTIFICATION_ID, builder.build()); 
} 

PHP發送信息和發送功能

require_once 'push_db_functions.php'; 
$dbf = new push_db_functions(); 
$id = '40'; 
$message = "Test Message"; 

$response = $dbf->sendMessage($id, $message); 
$response = json_decode($response); 
print_r($response); 

public function sendMessage($id, $message) { 
    $results = mysql_query("SELECT regid FROM test WHERE id = '$id'"); 
    $processed = mysql_fetch_row($results); 
    $url = 'https://android.googleapis.com/gcm/send'; 
    $apiKey = "AIzaSyD-xxx"; 
    $fields = array('registration_ids' => $processed, 'data' => array("message" => $message),); 
    $headers = array('Authorization: key=' . $apiKey, 'Content-Type: application/json'); 

    // open connection 
    $ch = curl_init(); 
    // set the url, number of POST vars, POST data 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 
    // execute post 
    $response = curl_exec($ch); 
    curl_close($ch); 
    return $response; 
} 

發送消息響應

stdClass Object ([multicast_id] => 5036849453049739126 [success] => 1 [failure] => 0 [canonical_ids] => 0 [results] => Array ([0] => stdClass Object ([message_id] => 0:1353799902066759%04108f12f9fd7ecd))) success 

我有我的數據庫2個REGID記錄,一個仿真器,一個用於手機的$ id 39和40,並改變他們因此將消息發送到任何設備。

數據庫記錄

id | regid 
39 | APA91bFyMJx4b0ddI........ Emulator 
40 | APA91bFhwEhHOClkg........ Phone 

的手機運行薑餅和程序寄存器和正常運行就可以了,除了不顯示在消息收到通知。

mDisplay = (TextView) findViewById(R.id.display); 
    registerReceiver(mHandleMessageReceiver, new IntentFilter(DISPLAY_MESSAGE_ACTION)); 
    final String regId = GCMRegistrar.getRegistrationId(this); 
    Log.d("registrationID", "::" + regId); 
    if (regId.equals("")) { 
     // automatically registers application on startup. 
     GCMRegistrar.register(this, SENDER_ID); 
    } 

任何幫助將不勝感激。

感謝

+0

因此事件被接收到編程您收到手機上的通知顯示,你只是遇到了麻煩的實際通知,並在通知欄顯示? –

+0

是的,這是程序收到正確的消息,但在通知欄 – Mike

+0

沒有通知顯示你怎麼稱呼generateNotification?模擬器和設備的版本是什麼? – Snicolas

回答

0

感謝您對大家,回答了這一問題。

我已經解決了這個問題:

在帳戶下的同步&我的手機,一般同步設置,我發現的後臺數據同步框沒有打勾。當設置所有工作正常。

我要問的問題,我向你道歉之前已經想通了這一點。

但是我希望這個答案可以是他人的援助。

相關問題