コード例 #1
0
        /// <summary>
        /// Puts a local notification to show calendar card
        /// </summary>
        /// <param name="dataItem">Data item.</param>
        private void UpdateNotificationForDataItem(IDataItem dataItem)
        {
            if (Log.IsLoggable(Constants.TAG, LogPriority.Verbose))
            {
                Log.Verbose(Constants.TAG, "Updating notification for IDataItem");
            }

            DataMapItem mapDataItem = DataMapItem.FromDataItem(dataItem);
            DataMap     data        = mapDataItem.DataMap;

            String description = data.GetString(Constants.DESCRIPTION);

            if (TextUtils.IsEmpty(description))
            {
                description = "";
            }
            else
            {
                // Add a space between the description and the time of the event
                description += " ";
            }

            String contentText;

            if (data.GetBoolean(Constants.ALL_DAY))
            {
                contentText = GetString(Resource.String.desc_all_day, description);
            }
            else
            {
                String startTime = DateFormat.GetTimeFormat(this).Format(new Date(data.GetLong(Constants.BEGIN)));
                String endTime   = DateFormat.GetTimeFormat(this).Format(new Date(data.GetLong(Constants.END)));
                contentText = GetString(Resource.String.desc_time_period, description, startTime, endTime);
            }

            Intent deleteOperation = new Intent(this, typeof(DeleteService));
            // Use a unique identifier for the delete action.

            String deleteAction = "action_delete" + dataItem.Uri.ToString() + sNotificationId;

            deleteOperation.SetAction(deleteAction);
            deleteOperation.SetData(dataItem.Uri);
            PendingIntent deleteIntent       = PendingIntent.GetService(this, 0, deleteOperation, PendingIntentFlags.OneShot);
            PendingIntent silentDeleteIntent = PendingIntent.GetService(this, 1, deleteOperation.PutExtra(Constants.EXTRA_SILENT, true), PendingIntentFlags.OneShot);

            Notification.Builder notificationBuilder = new Notification.Builder(this)
                                                       .SetContentTitle(data.GetString(Constants.TITLE))
                                                       .SetContentText(contentText)
                                                       .SetSmallIcon(Resource.Drawable.ic_launcher)
                                                       .AddAction(Resource.Drawable.ic_menu_delete, GetText(Resource.String.delete), deleteIntent)
                                                       .SetDeleteIntent(silentDeleteIntent)
                                                       .SetLocalOnly(true)
                                                       .SetPriority((int)NotificationPriority.Min);

            // Set the event owner's profile picture as the notification background
            Asset asset = data.GetAsset(Constants.PROFILE_PIC);

            if (asset != null)
            {
                if (mGoogleApiClient.IsConnected)
                {
                    IDataApiGetFdForAssetResult assetFdResult = WearableClass.DataApi.GetFdForAsset(mGoogleApiClient, asset).Await().JavaCast <IDataApiGetFdForAssetResult>();
                    if (assetFdResult.Status.IsSuccess)
                    {
                        Bitmap profilePic = BitmapFactory.DecodeStream(assetFdResult.InputStream);
                        notificationBuilder.Extend(new Notification.WearableExtender().SetBackground(profilePic));
                    }
                    else if (Log.IsLoggable(Constants.TAG, LogPriority.Debug))
                    {
                        Log.Debug(Constants.TAG, "asset fetch failed with StatusCode: " + assetFdResult.Status.StatusCode);
                    }
                }
                else
                {
                    Log.Error(Constants.TAG, "Failed to set notification background - Client disconnected from Google Play Services");
                }

                Notification card = notificationBuilder.Build();

                (GetSystemService(Context.NotificationService).JavaCast <NotificationManager>()).Notify(sNotificationId, card);

                sNotificationIdByDataItemUri.Add(dataItem.Uri, sNotificationId++);
            }
        }