我们有一个安卓手机应用程序,您可以在其中购买一段时间的停车票。现在,我们正计划将其与 Android Wear 集成。
我们在这里做的是:
- 我们希望用户在票证到期前 15 天收到通知。
- 为此,我们创建一个本地通知并使用 Alarm Manger 安排它。
- 此预定通知由 Android 广播接收器接收,并在移动设备上的 Android 通知部分显示此通知。
- 此外,此接收器调用意图服务以发送通知以进行佩戴。在这一步中,我们创建 googleApiClient 和 onConnected 回调,我们将数据发送到穿戴以显示通知。
- 佩戴时,用户可以查看通知,点击时,用户可以延长购票时间。此流程在通知点击后包含 3-4 个视图。
我们在第 4 步中遇到问题。大多数情况下,在第一次连接(通知)时,wear 不会显示通知,而在第二次连接(通知)时,wear 会同时显示第一个和第二个通知,之后它工作正常。
我们试图找出问题所在,但没有成功。下面是Receiver、Intent Service和穿戴端ListnerServices的代码片段,便于理解。
public class WearNotificationService extends IntentService {
private static final String TAG = "PhoneActivity";
private GoogleApiClient mGoogleApiClient;
public static String title;
public static String desc;
public static String data;
public WearNotificationService() {
super("WearNotificationService");
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, title +"--"+ desc , Toast.LENGTH_SHORT).show();
mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle connectionHint) {
sendNotification(title,desc,data);
Log.d(TAG, "onConnected: " + connectionHint);
}
@Override
public void onConnectionSuspended(int cause) {
Log.d(TAG, "onConnectionSuspended: " + cause);
}
}).addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.d(TAG, "onConnectionFailed: " + result);
}
}).addApi(Wearable.API).build();
mGoogleApiClient.connect();
}
@Override
protected void onHandleIntent(Intent intent) {
}
private void sendNotification(String title,String desc,String data) {
Log.e(TAG, "i am onConnectiond: ");
PutDataMapRequest dataMapRequest = PutDataMapRequest.create(Constants.PATH_NOTIFICATION);
dataMapRequest.getDataMap().putDouble(Constants.NOTIFICATION_TIMESTAMP, System.currentTimeMillis());
dataMapRequest.getDataMap().putString(Constants.KEY_TITLE, title);
dataMapRequest.getDataMap().putString(Constants.KEY_DESC, desc);
dataMapRequest.getDataMap().putString(Constants.KEY_DATA, data);
PutDataRequest putDataRequest = dataMapRequest.asPutDataRequest();
Wearable.DataApi.putDataItem(mGoogleApiClient, putDataRequest);
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (null != intent) {
String action = intent.getAction();
if (Constants.ACTION_DISMISS.equals(action)) {
dismissNotification();
}
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
for (DataEvent dataEvent : dataEvents) {
if (dataEvent.getType() == DataEvent.TYPE_CHANGED) {
if (Constants.PATH_NOTIFICATION.equals(dataEvent.getDataItem().getUri().getPath())) {
DataMapItem dataMapItem = DataMapItem.fromDataItem(dataEvent.getDataItem());
String title = dataMapItem.getDataMap().getString(Constants.KEY_TITLE);
String content = dataMapItem.getDataMap().getString(Constants.KEY_DESC);
String data = dataMapItem.getDataMap().getString(Constants.KEY_DATA);
String id = null;
try {
JSONObject obj = new JSONObject(data);
id = (String) obj.get("id");
} catch (JSONException e) {
e.printStackTrace();
}
sendNotification(title, content, data,id);
}
}
}
}
private void sendNotification(String title, String content, String data,String id) {
Intent notificationIntent = new Intent(this, HoursExtension.class);
Log.e("data1111", data);
HoursExtension.data = data;
HoursExtension.id = id;
PendingIntent notificationPendingIntent = PendingIntent.getActivity(this, 0, notificationIntent,0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle(title)
.setContentText(content)
.setContentIntent(notificationPendingIntent)
.extend(new NotificationCompat.WearableExtender().setBackground(BitmapFactory.decodeResource(getResources(), R.drawable.rtabg)))
;
Notification notification = builder.build();
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
notificationManagerCompat.notify(Integer.parseInt(id), notification);
}