Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.onemanleft.outwitters;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.drawable.BitmapDrawable;
- import com.onemanleft.outwitters.MainActivity;
- import com.onemanleft.outwitters.R;
- public class OutwittersNotificationManager {
- private static final int NOTIFICATION_ID = 1;
- public static void notifyNewTurn(Context context) {
- // Call this when the player gets a new turn
- getNotificationManager(context).notify(
- NOTIFICATION_ID,
- makeNotification(context).build()
- );
- }
- public static void updateTurnNotification(Context context) {
- // Call this when the player finishes a turn
- if (Game.hasPendingGame()) {
- getNotificationManager(context).notify(
- NOTIFICATION_ID,
- makeNotification(context).setOnlyAlertOnce(true).build()
- );
- }
- else{
- getNotificationManager(context).cancel(NOTIFICATION_ID);
- }
- }
- public static NotificationManager getNotificationManager(Context context) {
- return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
- }
- public static Notification.Builder makeNotification (Context context) {
- return new Notification.Builder(context)
- .setLargeIcon(getLargeIcon(context))
- .setContentTitle("Outwitters")
- .setSmallIcon(R.id.notificationIcon) // Or something, I don't know how Unity does drawables,
- // but I get the suspicion that it doesn't
- .setContentText(getContentText())
- .setWhen(getWhen())
- .setNumber(getNumberOfTurnsAvailable())
- .setContentIntent(PendingIntent.getActivity(
- context,
- 0,
- new Intent(context, MainActivity.class) //I don't know how Unity does Activities either
- /*
- * If you really want to get fancy, you can make the intent
- * navigate to the current turn
- *
- * This probably isn't exactly how you'd do this, but there's enough
- * documentation and Stack Overflow threads to make it work
- */
- .putExtra("GAME_ID_EXTRA", Long.MIN_VALUE /* some way of identifying the game */),
- /*
- * You'd also want to handle the intent by overriding
- * onNewIntent(Intent intent) in MainActivity
- * and have it take care of said fancy navigation
- *
- * This might not be possible with the way Unity does Activities,
- * but again, StackOverflow can probably cobble something together
- * if nothing else
- */
- PendingIntent.FLAG_UPDATE_CURRENT));
- }
- public static Bitmap getLargeIcon (Context context){
- return ((BitmapDrawable) context.getResources().getDrawable(R.drawable.ic_launcher)).getBitmap();
- // Or something, I don't know how Unity does drawables
- // Also, ignore that this method is deprecated as of API 22,
- // Google's API's are unbelievably stupid sometimes
- }
- public static String getContentText() {
- Game g = Game.getMostRecentGame();
- String message = "Your turn with " + g.opponentName;
- if (g.isMultiplayer) message += " and " + g.secondOpponentName;
- return message;
- }
- public static long getWhen(){
- return 0; // It's a really old game -- like REALLY old
- //
- // It's so old that if you cut its life into equal halves,
- // both of them can legally drink in the United States
- }
- public static int getNumberOfTurnsAvailable(){
- return Integer.MAX_VALUE; // They have a lot of games going on right now.
- }
- public static class Game{
- // For demonstration purposes only
- // Saying this is an oversimplification of what actually happens is an understatement,
- // but you get the idea and have probably already implemented this successfully
- // If you have not successfully implemented these methods and fields, then Alex
- // is most likely a wizard and should put on trial for abuse of arcane magic
- public boolean isMultiplayer;
- public String opponentName;
- public String secondOpponentName;
- public static Game getMostRecentGame(){
- return null; // This is probably already in Outwitters' code somewhere
- // Although hopefully not this exact method
- }
- public static boolean hasPendingGame(){
- return getMostRecentGame() != null; // Or something
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement