मैंने इस code के समान Android में एक NotificationListenerService बनाई है। मेरा ऐप नोटिफिकेशन को एक अलग विंडो में प्रदर्शित करता है। कब एक उपयोगकर्ता मेरी विंडो में अधिसूचना पर क्लिक करता है, संबंधित ऐप खोला जाता है।
public void onNotificationPosted(StatusBarNotification sbn) {
Bundle extras = sbn.getNotification().extras;
String title = getStringFromBundle(extras, "android.title");
String subText = getStringFromBundle(extras, "android.subText");
String text = getStringFromBundle(extras, "android.text");
String bigText = getStringFromBundle(extras, "android.bigText");
String array[] = { title, subText, text, bigText };
int progress = extras.getInt("android.progress", 0);
int progressMax = extras.getInt("android.progressMax", 0);
int int_array[] = { progress, progressMax };
notification_added(sbn, array, int_array, bitmap); //Adds the notification in a list
}
मैं कुंजी का उपयोग करके अधिसूचना खोलने का प्रयास करता हूं।
public void OpenNotification(String key) {
String keys[] = { key };
StatusBarNotification sbns[] = getActiveNotifications(keys);
for (StatusBarNotification sbn : sbns) {
try {
if (sbn == null) {
Log.i(TAG, "sbn is null");
continue;
}
/*
Notification n = sbn.getNotification();
if (n.contentIntent != null) {
PendingIntent pi = n.contentIntent;
if (pi != null) {
pi.send(this, 0, null);
}
}
*/
cancelNotification(key);
Intent intent = getPackageManager().getLaunchIntentForPackage(
sbn.getPackageName());
if (intent != null) {
Log.i(TAG, "Launching intent " + intent + " package name: "
+ sbn.getPackageName());
}
} catch (Exception e) {
}
}
}
उदाहरण के लिए, यदि ईमेल सूचना पर क्लिक किया जाता है, तो ऐप ईमेल ऐप लॉन्च करता है। लेकिन, यह सटीक ईमेल गतिविधि नहीं खोलता है। StatusBarNotification ऑब्जेक्ट से गतिविधि कैसे खोलें।
2 जवाब
एक कुंजी का उपयोग करके अधिसूचना खोलने के लिए।
public void OpenNotification(String key) {
String keys[] = { key };
StatusBarNotification sbns[] = getActiveNotifications(keys);
for (StatusBarNotification sbn : sbns) {
try {
if (sbn == null) {
Log.i(TAG, "sbn is null");
continue;
}
Notification n = sbn.getNotification();
if (n.contentIntent != null) {
PendingIntent pi = n.contentIntent;
if (pi != null) {
pi.send();
}
}
} catch (Exception e) {
}
}
}
YOURACTIVITY को उस गतिविधि से बदलें जिसे आप सूचना के क्लिक पर खोलना चाहते हैं
Intent intent = new Intent(getBaseContext(), YOURACTIVITY.class);
PendingIntent contentIntent = PendingIntent.getActivity(getBaseContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder b = new NotificationCompat.Builder(getBaseContext());
b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("Ticker")
.setContentTitle("title")
.setContentText("message")
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
.setContentIntent(contentIntent)
.setContentInfo("Info");
Random r = new Random();
int randomNo = r.nextInt(100000000 + 1);
NotificationManager notificationManager = (NotificationManager) getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(randomNo, b.build());
संबंधित सवाल
नए सवाल
java
जावा एक उच्च स्तरीय प्रोग्रामिंग भाषा है। इस टैग का उपयोग तब करें जब आपको भाषा का उपयोग करने या समझने में समस्या हो। इस टैग का उपयोग शायद ही कभी किया जाता है और इसका उपयोग अक्सर [वसंत], [वसंत-बूट], [जकार्ता-ई], [Android], [javafx], [हडूप], [श्रेणी] और [मावेन] के साथ किया जाता है।