Search in sources :

Example 16 with NotificationManagerCompat

use of android.support.v4.app.NotificationManagerCompat in project RSAndroidApp by RailwayStations.

the class NearbyBahnhofNotificationManager method onNotificationReady.

/**
     * Called back once the notification was built up ready.
     *
     * @param notification
     */
protected void onNotificationReady(Notification notification) {
    if (context == null)
        // we're already destroyed
        return;
    // Get an instance of the NotificationManager service
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
    // Build the notification and issues it with notification manager.
    notificationManager.notify(NOTIFICATION_ID, notification);
}
Also used : NotificationManagerCompat(android.support.v4.app.NotificationManagerCompat)

Example 17 with NotificationManagerCompat

use of android.support.v4.app.NotificationManagerCompat in project AndroidChromium by JackyAndroid.

the class MediaNotificationManager method updateNotification.

private void updateNotification() {
    if (mService == null)
        return;
    if (mMediaNotificationInfo == null)
        return;
    updateMediaSession();
    mNotificationBuilder = new NotificationCompat.Builder(mContext);
    setMediaStyleLayoutForNotificationBuilder(mNotificationBuilder);
    mNotificationBuilder.setSmallIcon(mMediaNotificationInfo.icon);
    mNotificationBuilder.setAutoCancel(false);
    mNotificationBuilder.setLocalOnly(true);
    if (mMediaNotificationInfo.supportsSwipeAway()) {
        mNotificationBuilder.setOngoing(!mMediaNotificationInfo.isPaused);
        mNotificationBuilder.setDeleteIntent(createPendingIntent(ListenerService.ACTION_SWIPE));
    }
    // TODO(avayvod) work out what we should do in this case. See https://crbug.com/585395.
    if (mMediaNotificationInfo.contentIntent != null) {
        mNotificationBuilder.setContentIntent(PendingIntent.getActivity(mContext, mMediaNotificationInfo.tabId, mMediaNotificationInfo.contentIntent, PendingIntent.FLAG_UPDATE_CURRENT));
    // Set FLAG_UPDATE_CURRENT so that the intent extras is updated, otherwise the
    // intent extras will stay the same for the same tab.
    }
    mNotificationBuilder.setVisibility(mMediaNotificationInfo.isPrivate ? NotificationCompat.VISIBILITY_PRIVATE : NotificationCompat.VISIBILITY_PUBLIC);
    Notification notification = mNotificationBuilder.build();
    // Moving it back to background allows the user to remove the notification.
    if (mMediaNotificationInfo.supportsSwipeAway() && mMediaNotificationInfo.isPaused) {
        mService.stopForeground(false);
        NotificationManagerCompat manager = NotificationManagerCompat.from(mContext);
        manager.notify(mMediaNotificationInfo.id, notification);
    } else {
        mService.startForeground(mMediaNotificationInfo.id, notification);
    }
}
Also used : NotificationManagerCompat(android.support.v4.app.NotificationManagerCompat) NotificationCompat(android.support.v7.app.NotificationCompat) Notification(android.app.Notification)

Example 18 with NotificationManagerCompat

use of android.support.v4.app.NotificationManagerCompat in project AndroidChromium by JackyAndroid.

the class MediaNotificationManager method clearNotification.

private void clearNotification() {
    if (mMediaNotificationInfo == null)
        return;
    NotificationManagerCompat manager = NotificationManagerCompat.from(mContext);
    manager.cancel(mMediaNotificationInfo.id);
    if (mMediaSession != null) {
        mMediaSession.setCallback(null);
        mMediaSession.setActive(false);
        mMediaSession.release();
        mMediaSession = null;
    }
    mContext.stopService(createIntent(mContext));
    mMediaNotificationInfo = null;
}
Also used : NotificationManagerCompat(android.support.v4.app.NotificationManagerCompat)

Example 19 with NotificationManagerCompat

use of android.support.v4.app.NotificationManagerCompat in project AndroidChromium by JackyAndroid.

the class DownloadManagerDelegate method addCompletedDownload.

/**
     * @see android.app.DownloadManager#addCompletedDownload(String, String, boolean, String,
     * String, long, boolean)
     */
protected long addCompletedDownload(String fileName, String description, String mimeType, String path, long length, String originalUrl, String referer, String downloadGuid) {
    DownloadManager manager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mContext);
    boolean useSystemNotification = !notificationManager.areNotificationsEnabled();
    long downloadId = -1;
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
        Class<?> c = manager.getClass();
        try {
            Class[] args = { String.class, String.class, boolean.class, String.class, String.class, long.class, boolean.class, Uri.class, Uri.class };
            Method method = c.getMethod("addCompletedDownload", args);
            Uri originalUri = Uri.parse(originalUrl);
            Uri refererUri = referer == null ? Uri.EMPTY : Uri.parse(referer);
            downloadId = (Long) method.invoke(manager, fileName, description, true, mimeType, path, length, useSystemNotification, originalUri, refererUri);
        } catch (SecurityException e) {
            Log.e(TAG, "Cannot access the needed method.");
        } catch (NoSuchMethodException e) {
            Log.e(TAG, "Cannot find the needed method.");
        } catch (InvocationTargetException e) {
            Log.e(TAG, "Error calling the needed method.");
        } catch (IllegalAccessException e) {
            Log.e(TAG, "Error accessing the needed method.");
        }
    } else {
        downloadId = manager.addCompletedDownload(fileName, description, true, mimeType, path, length, useSystemNotification);
    }
    addDownloadIdMapping(downloadId, downloadGuid);
    return downloadId;
}
Also used : NotificationManagerCompat(android.support.v4.app.NotificationManagerCompat) Method(java.lang.reflect.Method) DownloadManager(android.app.DownloadManager) Uri(android.net.Uri) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 20 with NotificationManagerCompat

use of android.support.v4.app.NotificationManagerCompat in project Small by wequick.

the class MainFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    // 启动一个带自定义转场动画的Activity
    // 需要用户在宿主提前占坑的地方:
    //  1. 转场动画相关anim资源
    Button button = (Button) rootView.findViewById(R.id.start_transition_activity_button);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainFragment.this.getContext(), TransitionActivity.class);
            startActivity(intent);
        }
    });
    /**
         * 以下代码测试:
         * 1. 成功发送通知, 在通知栏显示通知图标与信息
         * 2. 点击通知, 成功跳转指定Activity
         * 3. 在该Activity返回, 成功返回上一个界面
         * @see https://developer.android.com/training/notify-user/navigation.html#ExtendedNotification
         */
    // 方案一: 使用PendingIntent.getActivity构造PendingIntent, 发起一个通知。
    // 额外操作:
    //  1. 在 `stub` 模块放置 `smallIcon` 图片资源
    //  2. 使用 `Small.wrapIntent(intent)` 暗度插件意图
    button = (Button) rootView.findViewById(R.id.send_notification_special_button);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Context context = getContext();
            Intent onclickIntent = new Intent(context, NotifyResultActivity.class);
            onclickIntent.putExtra("notification_id", MY_NOTIFICATION_ID);
            //!< 增加这行代码
            Small.wrapIntent(onclickIntent);
            PendingIntent pi = PendingIntent.getActivity(context, 0, onclickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            Bitmap largeIcon = BitmapFactory.decodeResource(getContext().getResources(), // large icon的资源可以在插件里
            R.drawable.ic_large_notification);
            NotificationCompat.Builder nb = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_notification).setLargeIcon(largeIcon).setContentTitle("Small").setContentText("Click to start pending intent with PendingIntent.getActivity").setContentIntent(pi);
            NotificationManagerCompat nm = NotificationManagerCompat.from(context);
            nm.notify(MY_NOTIFICATION_ID, nb.build());
        }
    });
    // 方案二: 使用TaskStackBuilder构造PendingIntent, 发起一个通知
    // 额外操作:
    //   1. 在 `stub` 模块放置 `smallIcon` 图片资源
    //
    // 这里不需要手动修改意图, 因为 `Small` 对 `TaskStackBuilder` 进行了Hook, 自动完成wrapIntent
    button = (Button) rootView.findViewById(R.id.send_notification_taskstack_button);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Context context = getContext();
            Intent onclickIntent = new Intent(context, NotifyResultActivity.class);
            onclickIntent.putExtra("notification_id", MY_NOTIFICATION_ID);
            PendingIntent pi = TaskStackBuilder.create(context).addNextIntent(getActivity().getIntent()).addNextIntent(onclickIntent).getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
            Bitmap largeIcon = BitmapFactory.decodeResource(getContext().getResources(), // large icon的资源可以在插件里
            R.drawable.ic_large_notification);
            NotificationCompat.Builder nb = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_notification).setLargeIcon(largeIcon).setContentTitle("Small").setContentText("Click to start pending intent with TaskStackBuilder").setContentIntent(pi);
            NotificationManagerCompat nm = NotificationManagerCompat.from(context);
            nm.notify(MY_NOTIFICATION_ID, nb.build());
        }
    });
    // 本地服务
    button = (Button) rootView.findViewById(R.id.start_service_button);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getContext(), MyLocalService.class);
            getContext().startService(intent);
        }
    });
    button = (Button) rootView.findViewById(R.id.stop_service_button);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getContext(), MyLocalService.class);
            getContext().stopService(intent);
        }
    });
    // 远程服务
    mServiceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };
    button = (Button) rootView.findViewById(R.id.bind_remote_service_button);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getContext(), MyRemoteService.class);
            getContext().bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
        }
    });
    button = (Button) rootView.findViewById(R.id.unbind_remote_service_button);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            getContext().unbindService(mServiceConnection);
        }
    });
    // 广播
    button = (Button) rootView.findViewById(R.id.send_broadcast_button);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setAction("net.wequick.example.small.MyAction");
            getContext().sendBroadcast(intent);
        }
    });
    button = (Button) rootView.findViewById(R.id.get_content_button);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ContentResolver resolver = getContext().getContentResolver();
            Uri uri = Uri.parse("content://net.wequick.example.small/test");
            // Insert
            ContentValues values = new ContentValues();
            values.put("name", "T" + System.currentTimeMillis());
            resolver.insert(uri, values);
            // Query
            Cursor cursor = resolver.query(uri, null, null, null, "id desc");
            if (cursor == null) {
                return;
            }
            if (cursor.moveToFirst()) {
                String msg = "name in top record is: " + cursor.getString(1);
                Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
            }
        }
    });
    button = (Button) rootView.findViewById(R.id.start_remote_activity_button);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getContext(), MyRemoteActivity.class);
            startActivity(intent);
        }
    });
    return rootView;
}
Also used : Context(android.content.Context) ContentValues(android.content.ContentValues) ServiceConnection(android.content.ServiceConnection) TaskStackBuilder(android.support.v4.app.TaskStackBuilder) NotificationManagerCompat(android.support.v4.app.NotificationManagerCompat) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Cursor(android.database.Cursor) View(android.view.View) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver) Bitmap(android.graphics.Bitmap) IBinder(android.os.IBinder) Button(android.widget.Button) ComponentName(android.content.ComponentName) PendingIntent(android.app.PendingIntent)

Aggregations

NotificationManagerCompat (android.support.v4.app.NotificationManagerCompat)38 NotificationCompat (android.support.v4.app.NotificationCompat)17 PendingIntent (android.app.PendingIntent)15 Intent (android.content.Intent)15 SharedPreferences (android.content.SharedPreferences)7 PowerManager (android.os.PowerManager)7 Bitmap (android.graphics.Bitmap)6 AppSettings (com.klinker.android.twitter.settings.AppSettings)6 Notification (android.app.Notification)5 Context (android.content.Context)4 Cursor (android.database.Cursor)4 RemoteInput (android.support.v4.app.RemoteInput)4 Bundle (android.os.Bundle)3 ComponentName (android.content.ComponentName)2 ContentResolver (android.content.ContentResolver)2 ContentValues (android.content.ContentValues)2 Uri (android.net.Uri)2 Builder (android.support.v4.app.NotificationCompat.Builder)2 TaskStackBuilder (android.support.v4.app.TaskStackBuilder)2 ArrayList (java.util.ArrayList)2