[英]Sony Smartwatch 2 and push notifications
我試圖將Notification + Control示例訂閱到Google Cloud Messaging,以便直接將推送通知接收到手表。
我添加了permisions:
<permission android:name="com.example.myapp.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.sonymobile.smartconnect.extension.notificationsample.permission.C2D_MESSAGE" />
添加了接收器更新和服務:
<receiver android:name=".MyReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<!-- Receive the actual message -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.sonymobile.smartconnect.extension.notificationsample" />
</intent-filter>
<!-- Receive the registration id -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.sonymobile.smartconnect.extension.notificationsample" />
</intent-filter>
</receiver>
並使用此鏈接中的方法實現MyReceiver類
onReceive方法在首次注冊時才被調用。 它給我返回了tokenID,但是當我激活示例應用程序時,既沒有收到通知,也沒有調用它。
我已經使用正常的Android應用程序測試了發送方服務器,並且成功發送了通知
是否有向智能手表發送推送通知的提示?
謝謝!
更新 :現在我已經在我的問題上前進了,我已經將MyReceiver.java更改為:
公共類MyReceiver擴展了GCMBroadcastReceiver {
@Override
protected String getGCMIntentServiceClassName(Context context)
{
return RemoteNotificationIntentService.class.getName();
}
}
我已經實現了RemoteNotificationIntentService類:
公共類RemoteNotificationIntentService擴展了GCMBaseIntentService {公共靜態枚舉RemoteNotificationFields {RN_TITLE,RN_BODY,RN_TICKER,RN_SOUND,RN_SMALL_ICON,RN_LARGE_ICON,RN_LED_COLOR_ARGB}; 私有靜態HashMap FIELD_MAP;
private static final String APPLICATION_NAME = "Appverse Bank 4 Sw2";
public RemoteNotificationIntentService(String senderId) {
super(senderId);
System.out.println("init");
// TODO Auto-generated constructor stub
}
public RemoteNotificationIntentService() {
super("PUSH_NOTIFICATION_SERVICE");
// TODO Auto-generated constructor stub
System.out.println("init");
}
private HashMap<String, String> storeIntentExtras(Context context, Intent intent) {
try{
HashMap<String, String> returnValue = new HashMap<String, String>();
HashMap<String, String> JSONSerializable = new HashMap<String, String>();
for(String sFieldName:intent.getExtras().keySet()){
if(FIELD_MAP.containsKey(sFieldName)){
String sFieldType = FIELD_MAP.get(sFieldName);
returnValue.put(sFieldType, intent.getStringExtra(sFieldName));
JSONSerializable.put(sFieldType, intent.getStringExtra(sFieldName));
}else{
JSONSerializable.put(sFieldName, intent.getStringExtra(sFieldName));
}
}
//fill mandatory fields
if(!returnValue.containsKey(RemoteNotificationFields.RN_TITLE.toString())||returnValue.get(RemoteNotificationFields.RN_TITLE.toString()).trim().equals("")){returnValue.put(RemoteNotificationFields.RN_TITLE.toString(), APPLICATION_NAME);}
if(!returnValue.containsKey(RemoteNotificationFields.RN_TICKER.toString())||returnValue.get(RemoteNotificationFields.RN_TICKER.toString()).trim().equals("")){returnValue.put(RemoteNotificationFields.RN_TICKER.toString(), returnValue.get(RemoteNotificationFields.RN_TITLE.toString()));}
if(!returnValue.containsKey(RemoteNotificationFields.RN_SMALL_ICON.toString())||returnValue.get(RemoteNotificationFields.RN_SMALL_ICON.toString()).trim().equals("")){returnValue.put(RemoteNotificationFields.RN_SMALL_ICON.toString(), "icon");}
if(!returnValue.containsKey(RemoteNotificationFields.RN_LED_COLOR_ARGB.toString())||returnValue.get(RemoteNotificationFields.RN_LED_COLOR_ARGB.toString()).trim().equals("")){returnValue.put(RemoteNotificationFields.RN_LED_COLOR_ARGB.toString(), String.valueOf(Color.BLUE));}
JSONSerializable = null;
return returnValue;
}catch(Exception ex){}
return null;
}
@Override
protected void onMessage(Context context, Intent intent) {
try{
System.out.println("message!!!");
}catch(Exception ex){/*CANNOT LOG CAUSE CALLING LOGGER WILL PROMPT NULL POINTER EXCEPTION*/}
}
@Override
protected void onRegistered(Context context, String registrationId) {
try{
System.out.println("Register K");
}catch(Exception ex){
System.out.println("onRegistered "+ex.getMessage());
}
}
@Override
protected void onUnregistered(Context context, String registrationId) {
try{
System.out.println("Unregister K");
} catch(Exception ex){
System.out.println("onUnregistered "+ex.getMessage());
}
}
@Override
protected void onError(Context context, String error) {
System.out.println("error");
}
}
但它沒有在任何方法中輸入此方法,我得到此消息:
V / GCMBroadcastReceiver(4052):onReceive:com.google.android.c2dm.intent.RECEIVE
V / GCMBroadcastReceiver(4052):GCM IntentService類:com.sonymobile.smartconnect.extension.notificationsample.RemoteNotificationIntentService
V / GCMBaseIntentService(1825):獲取喚醒鎖
更新2
我最終實現了在類中獲取數據,我需要將服務添加到清單中
在正常的Android應用中,您是否能夠成功接收C2DM消息? 無論您是創建標准的Android應用程序還是SW2的Control擴展,接收C2DM通知都應該相同。 唯一的區別是,在收到C2DM通知后,對於SW2,然后將其添加到SW2的內容提供程序中,以將通知從手機推送到手表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.