Notification depuis un service.
Bonjour à tous!
Je viens à vous car j'ai un petit problème pour envoyer une notification depuis un background service.
En effet j'ai un service qui travail avec un serveur php distant pour savoir si des infos sont dispo et j'aimerai une notification quand c'est le cas. J'arrive à récupérer mes infos distantes mais impossible d'afficher une notification sans que tous n'explose :p
Du coup je me pose 2 questions,
Est-il "propre" d'afficher une notification depuis un service ?
Si oui pourquoi mon code ne marche pas?
J'ai fait un tour de google et j'ai vu que certain propose du code pour le faire, mais ça ne semble pas marcher chez moi...
Des que j'essaie de créer une notification j'ai une erreur Java.lang.NullPointerException sur la ligne suivante :
Code:
1 2
|
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); |
Je vous montre ce que j'ai fait :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
package com.example.service;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import com.example.f_s.MainActivity;
import com.example.f_s.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
public class BackgroundService extends Service{
Context context;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
public void onCreate() {
super.onCreate();
start();
}
@Override
public void onDestroy() {
super.onDestroy();
}
public void start(){
OutputStream sortieSocket;
PrintStream bw;
KeyStore trustStore;
BufferedReader br;
String str = "";
try {
// Configuration de la socket pour accepter le ceritificat auto signé
trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
MySSLSocketFactory sf = new MySSLSocketFactory(trustStore);
sf.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
// Création de la socket
Socket so = sf.createSocket();
SocketAddress soA = new InetSocketAddress("192.168.1.90" , 8098);
// Connection au serveur
so.connect(soA);
// Récupération des flux sur la socket
sortieSocket = so.getOutputStream();
bw = new PrintStream(sortieSocket);
br = new BufferedReader(new InputStreamReader(so.getInputStream()));
// Envoie des identifiants au serveur
bw.print("zefzefzfe");
bw.flush();
// Lecture des données du serveur
while(str != null){
str = br.readLine();
System.out.println("Réponse : "+str);
createNotification();
}
// Fermeture des ressources
bw.close();
br.close();
so.close();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (UnrecoverableKeyException e) {
e.printStackTrace();
}
}
public void createNotification(){
NotificationManager notificationManager = null;
try {
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
} catch (Exception e) {
System.out.println(e.toString());
}
int icon = R.drawable.ic_launcher;
CharSequence tickerText = "Pet Parrot";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Context context = getApplicationContext();
CharSequence contentTitle = "Hungry!";
CharSequence contentText = "your parrot food meter is: ";
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationManager.notify(1, notification);
}
} |
Voici mon manifest :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.f_s"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.example.service.BACKGROUNDSERVICE_PERMISSION"/>
<permission android:name="com.example.service.BACKGROUNDSERVICE_PERMISSION" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.f_s.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.f_s.Test"
android:label="@string/title_activity_test" >
</activity>
<activity
android:name="com.example.f_s.LoginActivity"
android:label="Connexion"
android:windowSoftInputMode="adjustResize|stateVisible" >
</activity>
<service
android:exported="true"
android:name="com.example.service.BackgroundService"
android:permission="com.example.service.BACKGROUNDSERVICE_PERMISSION"
android:enabled="true"
/>
<receiver android:name="com.example.service.BackgroundServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
</application>
</manifest> |
Merci d'avance.