- Home /
Notification on Android
I'm trying to send a notification via Android but it isn't working properly.
Here is my java code:
public class MainActivity extends UnityPlayerNativeActivity {
private static Context context;
private static Notification mNotification;
private static NotificationManager mNotificationManager;
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
context = this;
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
context = this;
}
public static String GetString() {
return "java: hello";
}
public static void NotifyUser()
{
Log.d("yoob", "NotifyUser");
Log.d("yoob", "Start");
Resources res = context.getResources();
int icon = res.getIdentifier("yoobsicon", "drawable", context.getPackageName());
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context).setSmallIcon(icon)
.setContentTitle("Yoob diz:")
.setContentText("Fome da mizera!");
mNotification = mBuilder.build();
Log.d("yoob", "middle");
mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(123, mNotification);
Log.d("yoob", "end");
}
}
And here's my Unity code:
using UnityEngine;
using System.Collections;
public class NumberExample : MonoBehaviour {
public string message = "mensagem";
public TextMesh textMesh;
private int count = 0;
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidJavaClass pluginPlugAndPlay;
#endif
void Start ()
{
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidJNI.AttachCurrentThread();
pluginPlugAndPlay = new AndroidJavaClass("com.cesar.yoobs.MainActivity");
#endif
//textMesh.text = message;
}
void Update()
{
#if UNITY_ANDROID && !UNITY_EDITOR
message = pluginPlugAndPlay.CallStatic<string>("GetString");
count++;
if(count == 100) pluginPlugAndPlay.CallStatic("NotifyUser");
#endif
message = message + " " + count;
textMesh.text = message;
}
}
If I build my java code directly to android it works fine and I get the proper notification. On Eclipse I get a nullPointerException which I think it has to do with him not finding the icon when on Unity. I have everything set in Plugins/Android/res as I've seen other people mentioning here. Is there anything else I should be doing?
Thanks in advance.
Hi there,
I just want to try to work out the context of your question, so I can help you get exactly what you are after.
Are you trying to produce a notification ( http://developer.android.com/guide/topics/ui/notifiers/notifications.html ) based on something happening in your game?
What kind of event are you looking to trigger an event from. What happens in your game? What kind of game is it exactly? Why a notification rather than just displaying something on the screen? (The reason I ask is, in my experience, a notification is usually sent by a background application like an email client to prompt the user to bring the application to the foreground.) What should happen when they tap the notification?
Dave
This post should help also:
http://forum.unity3d.com/threads/push-notifications-on-unity-android.126528/
Your answer
Follow this Question
Related Questions
Custom push notification sound in android build doesn't work 0 Answers
Android Push Notifications stopped working using GMS? 0 Answers
Send Push Notifications to Android Device 5 Answers
Free Android local notification plugin 1 Answer
How to call an android notification plugin if it's not the main activity? 1 Answer