2012-11-22 4 views
1

내가 여러 등록 ID에 알림을 보낼 PHP 코드를 구현하지만 많이 검색 한 후, 나는 내 코드 여기 에 그것을 구현하는 방법을 보여줄 수 GCMIntentService & GCMBroadcastReciever에 대한 구현을 찾을 수 없습니다 PHP 코드여러 등록 ID로 알림을 보내고 GCM을 사용하여 알림을받는 방법은 무엇입니까?

입니다
$url = 'https://android.googleapis.com/gcm/send'; 

$fields = array(
     'registration_ids' => $apiKey, 
     //'data'    => array("message" => $message), 
     'data' =>$message , 
); 

$headers = array(
     'Authorization: key=' . "AIzaSyBCzOGxXyvvo8-ZSzesOsTIYa-7Ua64_SM", 
     '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 
$result = curl_exec($ch); 

// Close connection 
curl_close($ch); 

echo $result; 
} 

여기에 내가

package guc.edu.iremote; 

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.os.PowerManager; 
import android.os.PowerManager.WakeLock; 
import android.support.v4.app.NotificationCompat; 
import android.util.Log; 

import com.google.android.gcm.GCMBaseIntentService; 

public class GCMIntentService extends GCMBaseIntentService { 

private static final String TAG = "===GCMIntentService==="; 
private static final String senderId = "559753615670"; 
String message; 

public GCMIntentService() { 
    super(senderId); 
} 

@Override 
protected void onRegistered(Context arg0, String registrationId) { 
    Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER"); 
    // sets the app name in the intent 
    registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); 
    registrationIntent.putExtra("sender", senderId); 
    startService(registrationIntent); 
    Log.i(TAG, "Device registered: regId = " + registrationId); 

} 

@Override 
protected void onUnregistered(Context arg0, String arg1) { 
    Intent unregIntent = new Intent("com.google.android.c2dm.intent.UNREGISTER"); 
    unregIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); 
    startService(unregIntent); 
    Log.i(TAG, "unregistered = " + arg1); 
} 

@Override 
protected void onMessage(Context context, Intent intent) { 
    intent.getStringExtra("message"); 

    Intent notificationIntent=new Intent(context,this.getApplicationContext().getClass()); 
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
    WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");  
    wl.acquire(); 
generateNotification(context, message, notificationIntent); 



} 

@SuppressWarnings("deprecation") 
private static void generateNotification(Context context, String message, Intent notificationIntent) { 
    int icon = R.drawable.ic_launcher; 
    long when = System.currentTimeMillis(); 


    NotificationManager notificationManager = (NotificationManager) 
      context.getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(icon, message, when); 

    notificationManager.notify(0, notification); 

    String title = context.getString(R.string.app_name); 
    // set intent so it does not start a new activity 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
      Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent intent =PendingIntent.getActivity(context, 0, notificationIntent,  PendingIntent.FLAG_UPDATE_CURRENT); 
    notification.setLatestEventInfo(context, title, message, intent); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    notificationManager.notify(0, notification); 
    notification.defaults |= Notification.DEFAULT_SOUND; 
    notification.defaults |= Notification.DEFAULT_VIBRATE; 
    notification.ledARGB = 0xff00ff00; 
notification.ledOnMS = 300; 
notification.ledOffMS = 1000; 
notification.flags |= Notification.FLAG_SHOW_LIGHTS; 
} 

@Override 
protected void onError(Context arg0, String errorId) { 
    Log.i(TAG, "Received error: " + errorId); 
} 

@Override 
protected boolean onRecoverableError(Context context, String errorId) { 
    return super.onRecoverableError(context, errorId); 
} 
} 

그래서 누구든지 내 안드로이드 코드에서 잘못 된 또는 누락 무엇을 도와 주실 래요 구현 내 안드로이드 클래스?

+0

왜 C2dm을 사용합니까? 당신은 GCM (구글 클라우드 메시지)를 사용해야합니다 .. –

답변

1

PHP에서 regids와 API 키의 방향이 잘못되었습니다. 다음과 같아야합니다.

$registrationIDs = array(); 
/* fetch values */ 
while ($stmt2->fetch()) { 
    $regidOne = $dev_i; // Filling the array with the regids 
    $registrationIDs[] = $regidOne; 
} 
$stmt2->close(); 
$fields = array(
      'registration_ids' => $registrationIDs, 
      'data' => array("message" => $message), 
      'delay_while_idle'=> false, 
      'collapse_key'=>"".$randomNum."" 
      ); 
    $headers = array(
     'Authorization: key=' . $apiKey, 
     'Content-Type: application/json' 
     ); 

    // Open connection 
    $ch = curl_init(); 
+0

괜찮아요,하지만 내 주요 문제는 안드로이드에 내가 안드로이드에 그것을 구현하는 방법을 모르는 안드로이드에 있습니다 –

관련 문제