├── localnotification ├── gradle.conf ├── AndroidManifest.conf └── src │ ├── LocalNotificationReceiver.java │ └── GodotLocalNotification.java ├── LICENSE └── README.md /localnotification/gradle.conf: -------------------------------------------------------------------------------- 1 | [dependencies] 2 | implementation 'androidx.appcompat:appcompat:1.1.0' 3 | -------------------------------------------------------------------------------- /localnotification/AndroidManifest.conf: -------------------------------------------------------------------------------- 1 | [application] 2 | 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Velmurugan R 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /localnotification/src/LocalNotificationReceiver.java: -------------------------------------------------------------------------------- 1 | package org.godotengine.godot; 2 | 3 | import android.app.Notification; 4 | import android.app.NotificationChannel; 5 | import android.app.NotificationManager; 6 | import android.app.PendingIntent; 7 | import android.content.BroadcastReceiver; 8 | import android.content.Context; 9 | import android.content.Intent; 10 | import android.graphics.Color; 11 | import android.os.Build; 12 | import androidx.core.app.NotificationCompat; 13 | import android.util.Log; 14 | import android.net.Uri; 15 | import android.media.RingtoneManager; 16 | 17 | 18 | public class LocalNotificationReceiver extends BroadcastReceiver { 19 | private static final String TAG = "Notification"; 20 | private static final String DEFAULT_CHANNEL_ID = "10001"; 21 | private static final String DEFAULT_CHANNEL_NAME = "Notify"; 22 | 23 | @Override 24 | public void onReceive(Context context, Intent intent) { 25 | int notificationId = intent.getIntExtra("notification_id", 0); 26 | String message = intent.getStringExtra("message"); 27 | String title = intent.getStringExtra("title"); 28 | Log.i(TAG, "Receive notification: "+message); 29 | 30 | Intent intent2 = new Intent(context, com.godot.game.GodotApp.class); 31 | intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 32 | PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent2, 33 | 0); 34 | 35 | NotificationCompat.Builder builder = new NotificationCompat.Builder(context,DEFAULT_CHANNEL_ID); 36 | builder.setContentTitle(title); 37 | builder.setContentText(message); 38 | builder.setSmallIcon(R.drawable.icon); 39 | builder.setTicker(message); 40 | builder.setAutoCancel(true); 41 | builder.setDefaults(Notification.DEFAULT_ALL); 42 | builder.setContentIntent(pendingIntent); 43 | builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); 44 | 45 | // builder.setColor(0xff0000ff); 46 | Notification notification = builder.build(); 47 | 48 | NotificationManager manager = (NotificationManager) context 49 | .getSystemService(Context.NOTIFICATION_SERVICE); 50 | 51 | // notification channel 52 | NotificationChannel nc = null; 53 | if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { 54 | nc = new NotificationChannel(DEFAULT_CHANNEL_ID, DEFAULT_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH); 55 | nc.enableLights(true); 56 | nc.enableVibration(true); 57 | nc.setLightColor(Color.GRAY); 58 | nc.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); 59 | } 60 | 61 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 62 | manager.createNotificationChannel(nc); 63 | } 64 | manager.notify(notificationId, notification); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GodotLocalNotification 2 | This is Android local notification module for [Godot Engine](https://github.com/okamstudio/godot "Godot Engine") 3,2+. For older version use [DrMoriarty](https://github.com/DrMoriarty/godot-local-notification "DrMoriarty/godot-local-notificatoin")'s custom module. 3 | 4 | ## How to use 5 | - Enable "Android Custom Template" in your project (follow this [Official Documentation](https://docs.godotengine.org/en/latest/getting_started/workflow/export/android_custom_build.html "Official Documentation")). 6 | - Download or Clone this repository 7 | - Drop the `localnotification` directory inside `res://android/` directory 8 | - Add this string inside your Project Setting->Android->Modules. (use comma to separate more than one modules) 9 | 10 | org/godotengine/godot/GodotLocalNotification 11 | 12 | ------------ 13 | 14 | ## Custom notification color 15 | - Go to the directory `res://android/localnotification/src` 16 | - Open `LocalNotificationReceiver.java` file 17 | - Find `builder.setColor(0xff0000ff)` line, uncomment it and use custom `argb` color values. 18 | ### How to change ARGB values 19 | 0xff0000ff is Blue color 20 | (Here, 0x**ff**0000ff - alpha; 21 | 0xff**00**00ff - red; 22 | 0xff00**00**ff - green; 23 | 0xff0000**ff** - blue) 24 | 25 | ------------ 26 | 27 | ### Do you love this module? 28 | If you want to support me, just download my puzzle game [cress](http://bit.ly/cresspuzzle "cress on Google play"), I made using this module (You can check out whether this module working or not, May be) 29 | 30 | ------------ 31 | 32 | ## Reference 33 | ```python 34 | # Show Banner 35 | # @param string message The notification message 36 | # @param sting title Notification title 37 | # @param int interval The time out interval in seconds 38 | # @param int tag The notification tag (Use the same id to override previous notification) 39 | show_local_notification(message, title, interval, tag) 40 | 41 | 42 | # Cancel the shown notificaton with tag 43 | # @param tag The tag of the notification 44 | cancel(tag) 45 | 46 | 47 | # Cancel all shown notifications having different tags 48 | cancel_all() 49 | 50 | 51 | # Stop last fired notification 52 | stop_last_notification() 53 | ``` 54 | 55 | ------------ 56 | 57 | 58 | ## Example: 59 | ```python 60 | var ln 61 | func _ready(): 62 | if(Engine.has_singleton("GodotLocalNotification")): 63 | ln = Engine.get_singleton("GodotLocalNotification") 64 | 65 | #events 66 | func _on_Button_pressed(): 67 | var message = "Hola..!!"+ str(OS.get_time()) 68 | var interval = 5 # 5 seconds 69 | var tag = 1 # tag is 1 70 | ln.show_local_notification(message,"My title 1",interval,tag) 71 | 72 | func _on_Button2_pressed(): 73 | var message = "Hello ..!!"+ str(OS.get_time()) 74 | var interval = 60 # 1 minute 75 | var tag = 2 # tag is 2 76 | ln.show_local_notification(message,"My title 2",interval,tag) 77 | 78 | func _on_Button_Cancel_One_pressed(): 79 | var tag = 1 #Close the shown notification which have tag 1 80 | ln.cancel(tag) 81 | 82 | func _on_Button_Cancel_All_pressed(): 83 | ln.cancel_all() #this will cancel all notifications having different tags 84 | 85 | func _on_Button_stop_last_pressed(): 86 | ln.stop_last_notification() #This will stop recently fired notification before the interval 87 | 88 | ``` 89 | 90 | ------------ 91 | -------------------------------------------------------------------------------- /localnotification/src/GodotLocalNotification.java: -------------------------------------------------------------------------------- 1 | package org.godotengine.godot; 2 | 3 | import android.app.NotificationManager; 4 | import android.app.Activity; 5 | import android.app.PendingIntent; 6 | import android.app.AlarmManager; 7 | import android.content.Intent; 8 | import android.util.Log; 9 | import java.util.Map; 10 | import java.util.List; 11 | import java.util.Arrays; 12 | import java.util.Calendar; 13 | import org.json.JSONObject; 14 | import org.json.JSONArray; 15 | import org.json.JSONException; 16 | import android.content.Context; 17 | 18 | public class GodotLocalNotification extends Godot.SingletonBase { 19 | 20 | private Godot activity = null; 21 | private PendingIntent sender = null; 22 | static public Godot.SingletonBase initialize(Activity p_activity) 23 | { 24 | return new GodotLocalNotification(p_activity); 25 | } 26 | 27 | public GodotLocalNotification(Activity p_activity) 28 | { 29 | registerClass("GodotLocalNotification", new String[]{"init", "show_local_notification", "isInited", "isEnabled", "register_remote_notification", "get_device_token","cancel","cancel_all","stop_last_notification"}); 30 | activity = (Godot)p_activity; 31 | } 32 | 33 | // Public methods 34 | 35 | public void init() { 36 | } 37 | 38 | public boolean isInited() { 39 | return true; 40 | } 41 | 42 | public boolean isEnabled() { 43 | return true; 44 | } 45 | 46 | public void show_local_notification(String message, String title, int interval, int tag){ 47 | showLocalNotification(message, title, interval, tag); 48 | } 49 | 50 | public void stop_last_notification(){ 51 | if (sender != null){ 52 | AlarmManager am = (AlarmManager)activity.getSystemService(activity.ALARM_SERVICE); 53 | am.cancel(sender); 54 | } 55 | } 56 | 57 | public void cancel_all(){ 58 | NotificationManager manager = (NotificationManager)activity.getSystemService(Context.NOTIFICATION_SERVICE); 59 | manager.cancelAll(); 60 | } 61 | 62 | public void cancel(int id){ 63 | NotificationManager manager = (NotificationManager)activity.getSystemService(Context.NOTIFICATION_SERVICE); 64 | manager.cancel(id); 65 | } 66 | 67 | private void showLocalNotification(String message, String title, int interval, int tag) { 68 | if(interval <= 0) return; 69 | sender = getPendingIntent(message, title, tag); 70 | Calendar calendar = Calendar.getInstance(); 71 | calendar.setTimeInMillis(System.currentTimeMillis()); 72 | calendar.add(Calendar.SECOND, interval); 73 | 74 | AlarmManager am = (AlarmManager)activity.getSystemService(activity.ALARM_SERVICE); 75 | am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender); 76 | } 77 | 78 | public void register_remote_notification() { 79 | } 80 | 81 | public String get_device_token() { 82 | return ""; 83 | } 84 | 85 | // Internal methods 86 | 87 | private PendingIntent getPendingIntent(String message, String title, int tag) { 88 | Intent i = new Intent(activity.getApplicationContext(), LocalNotificationReceiver.class); 89 | i.putExtra("notification_id", tag); 90 | i.putExtra("message", message); 91 | i.putExtra("title", title); 92 | PendingIntent sender = PendingIntent.getBroadcast(activity, tag, i, PendingIntent.FLAG_UPDATE_CURRENT); 93 | return sender; 94 | } 95 | 96 | @Override protected void onMainActivityResult (int requestCode, int resultCode, Intent data) 97 | { 98 | } 99 | 100 | } 101 | --------------------------------------------------------------------------------