app/src/main/java/fr/neamar/lolgamedata/service/NotificationService.java
/**
     * Create and show a simple notification containing the received GCM message.
     */
Create and show a simple notification containing the received GCM message.
Create and show a simple notification containing the received GCM message .
private void displayNotification(Account account, long gameId, int mapId) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        Intent intent = new Intent(this, GameActivity.class);
        intent.putExtra("account", account);
        intent.putExtra("source", "notification");
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_launcher_transparent_white)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_transparent))
                .setContentTitle(String.format(getString(R.string.welcome_to), getString(GameActivity.getMapName(mapId))))
                .setContentText(String.format(getString(R.string.player_is_in_game), account.summonerName))
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);
        if (prefs.getBoolean("notifications_new_game_vibrate", true)) {
            notificationBuilder.setVibrate(new long[]{1000, 1000});
        }
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
            Uri notificationUri = Uri.parse(prefs.getString("notifications_new_game_ringtone", Settings.System.DEFAULT_NOTIFICATION_URI.toString()));
            notificationBuilder.setSound(notificationUri);
        }
        NotificationManager notificationManager = getNotificationManager();
        boolean unableToDisplay = false;
        if (prefs.getBoolean("notifications_new_game", true)) {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                            getString(R.string.channel_in_game_notifications),
                            NotificationManager.IMPORTANCE_DEFAULT);
                    notificationManager.createNotificationChannel(channel);
                }
                if (prefs.getLong("last_viewed_game", -1) == gameId) {
                    Log.i(TAG, "Skipping notification display, game is already on screen.");
                }
                else {
                    notificationManager.notify(Long.toString(gameId).hashCode(), notificationBuilder.build());
                }
            } catch (RuntimeException e) {
                unableToDisplay = true;
            }
            Tracker.trackNotificationDisplayed(this, account, mapId, getString(GameActivity.getMapName(mapId)), gameId, unableToDisplay);
        }
    }
private void displayNotification ( Account account , long gameId , int mapId ) { SharedPreferences prefs = PreferenceManager . getDefaultSharedPreferences ( this ) ; Intent intent = new Intent ( this , GameActivity . class ) ; intent . putExtra ( "account" , account ) ; intent . putExtra ( "source" , "notification" ) ; intent . addFlags ( Intent . FLAG_ACTIVITY_CLEAR_TOP ) ; PendingIntent pendingIntent = PendingIntent . getActivity ( this , 0 /* Request code */ , intent , PendingIntent . FLAG_ONE_SHOT ) ; NotificationCompat . Builder notificationBuilder = new NotificationCompat . Builder ( this , CHANNEL_ID ) . setSmallIcon ( R . drawable . ic_launcher_transparent_white ) . setLargeIcon ( BitmapFactory . decodeResource ( getResources ( ) , R . drawable . ic_launcher_transparent ) ) . setContentTitle ( String . format ( getString ( R . string . welcome_to ) , getString ( GameActivity . getMapName ( mapId ) ) ) ) . setContentText ( String . format ( getString ( R . string . player_is_in_game ) , account . summonerName ) ) . setAutoCancel ( true ) . setContentIntent ( pendingIntent ) ; if ( prefs . getBoolean ( "notifications_new_game_vibrate" , true ) ) { notificationBuilder . setVibrate ( new long [ ] { 1000 , 1000 } ) ; } if ( Build . VERSION . SDK_INT == Build . VERSION_CODES . M ) { Uri notificationUri = Uri . parse ( prefs . getString ( "notifications_new_game_ringtone" , Settings . System . DEFAULT_NOTIFICATION_URI . toString ( ) ) ) ; notificationBuilder . setSound ( notificationUri ) ; } NotificationManager notificationManager = getNotificationManager ( ) ; boolean unableToDisplay = false ; if ( prefs . getBoolean ( "notifications_new_game" , true ) ) { try { if ( Build . VERSION . SDK_INT >= Build . VERSION_CODES . O ) { NotificationChannel channel = new NotificationChannel ( CHANNEL_ID , getString ( R . string . channel_in_game_notifications ) , NotificationManager . IMPORTANCE_DEFAULT ) ; notificationManager . createNotificationChannel ( channel ) ; } if ( prefs . getLong ( "last_viewed_game" , - 1 ) == gameId ) { Log . i ( TAG , "Skipping notification display, game is already on screen." ) ; } else { notificationManager . notify ( Long . toString ( gameId ) . hashCode ( ) , notificationBuilder . build ( ) ) ; } } catch ( RuntimeException e ) { unableToDisplay = true ; } Tracker . trackNotificationDisplayed ( this , account , mapId , getString ( GameActivity . getMapName ( mapId ) ) , gameId , unableToDisplay ) ; } }
Create and show a simple notification containing the received GCM message.
Create and show a simple notification containing the received GCM message .
// Most likely, the ringtone doesn't exist anymore. // Used to happen only in Android 6.0, so the notification part is skipped on API M. // I'm keeping the try/catch just in case ;)