2012-11-25 42 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

感谢您对大家,回答了这一问题。

我已经解决了这个问题:

在帐户下的同步&我的手机,一般同步设置,我发现的后台数据同步框没有打勾。当设置所有工作正常。

我要问的问题,我向你道歉之前已经想通了这一点。

但是我希望这个答案可以是他人的援助。

相关问题