我按照教程中的所有步骤操作。 tutorial android Push notification
我的设备使用GCM Id在drupal服务器上成功注册。
我的问题是,从服务器发送推送通知时,它会显示“成功发送通知”#39; ,但此通知未通过电话接收。
任何人都可以建议我该怎么做....
答案 0 :(得分:0)
最后我发现了我的错误。
我正在尝试在广播接收器中获取通知消息,但我在此服务onMessage(上下文上下文,意图数据)中获取消息是错误的。
public class GCMIntentService extends GCMBaseIntentService{
private static final String TAG = "GCMIntentService";
public GCMIntentService() {
super(SENDER_ID);
}
@Override
protected void onError(Context arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
protected void onMessage(Context context, Intent data) {
Log.i(TAG, "new message= ");
String message = data.getExtras().getString("message");
/*if (message.equals("") ) {
//message="hellow senha , this is vc circle notification. You can check how it display long or short...";
generateNotification(context, message);
}else{
generateNotification(context, message);
}*/
//message="hellow senha , this is vc circle notification. You can check how it display long or short...";
generateNotification(context, message);
}
private void generateNotification(Context context, String message) {
int icon = R.drawable.vc_placeholder;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, SplashScreenActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context);
Notification notification = mBuilder.setSmallIcon(icon).setTicker("VCCircle").setWhen(when)
.setContentTitle(context.getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentIntent(resultPendingIntent)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.vc_placeholder))
.setContentText(message).build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
{
// Wake Android Device when notification received
PowerManager pm = (PowerManager) context
.getSystemService(Context.POWER_SERVICE);
final PowerManager.WakeLock mWakelock = pm.newWakeLock(
PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP, "GCM_PUSH");
mWakelock.acquire();
// Timer before putting Android Device to sleep mode.
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
mWakelock.release();
}
};
timer.schedule(task, 5000);
}
}
@Override
protected void onRegistered(Context context, String registrationId) {
Log.i(TAG, "Device registered: regId = " + registrationId);
displayMessage(context, getString(R.string.gcm_registered));
boolean te=ServerUtilities.register(context, registrationId);
System.out.println("Device Register : "+te);
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
// TODO Auto-generated method stub
}
}