سؤال

يمكنني العثور على طريقة لإرسال المعلمات إلى نشاطي من إخطاري.

لدي خدمة تخلق إشعارا. عند النقر فوق المستخدم على الإخطار، أريد فتح نشاطي الرئيسي مع بعض المعلمات الخاصة. على سبيل المثال معرف عنصر، لذلك يمكن أن يقوم نشاطي بتحميل وعرض تفاصيل عناصر خاصة. أكثر تحديدا، أقوم بتنزيل ملف، وعند تحميل الملف، أريد أن يكون للإشعار بمثابة نية عند النقر فوقه يفتح نشاطي في وضع خاص. لقد حاولت استخدام putExtra على نيتي، ولكن لا يبدو أن استخراجها، لذلك أعتقد أنني أفعل ذلك خطأ.

الرمز من خدمتي التي تنشئ الإشعارات:

        // construct the Notification object.
     final Notification notif = new Notification(R.drawable.icon, tickerText, System.currentTimeMillis());


    final RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification_layout);
    contentView.setImageViewResource(R.id.image, R.drawable.icon);
    contentView.setTextViewText(R.id.text, tickerText);
    contentView.setProgressBar(R.id.progress,100,0, false);
    notif.contentView = contentView;        

    Intent notificationIntent = new Intent(context, Main.class);
    notificationIntent.putExtra("item_id", "1001"); // <-- HERE I PUT THE EXTRA VALUE
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notif.contentIntent = contentIntent;

    nm.notify(id, notif);

الرمز من نشاطي يحاول جلب المعلمة الإضافية من الإخطار:

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);


    Bundle extras = getIntent().getExtras();
    if(extras != null){
        Log.i( "dd","Extra:" + extras.getString("item_id") );
    }

الإضافات هي دائما لاغية ولا أحصل على أي شيء في السجل الخاص بي.

راجع للشغل ... onCreate يتم تشغيله فقط عندما يبدأ نشاطي، إذا بدأ نشاطي بالفعل، فأنا أرغب أيضا في جمع الإضافات وتقديم نشاطي وفقا لمعود Item_ID.

أيه أفكار؟

هل كانت مفيدة؟

المحلول

إلقاء نظرة على هذا الدليل (خلق إشعار) وإلى عينات Apidemos "StatureBarnotifications" و "إعلانات".

لإدارة إذا كان النشاط يعمل بالفعل لديك طريقتان:

  1. يضيف Flag_activity_single_top. علم بقصد عند تشغيل النشاط، ثم في تنفيذ فئة النشاط onwintent (نية نية) معالج الأحداث، بهذه الطريقة يمكنك الوصول إلى النية الجديدة التي تم استدعاؤها للنشاط (وهذا ليس هو نفسه فقط استدعاء getintent ()، وهذا سيؤدي دائما إلى إرجاع النية الأولى التي أطلقت نشاطك.

  2. نفس رقم واحد، ولكن بدلا من إضافة علامة إلى النية يجب أن تضيف "singletop" في نشاطك Androidmanifest.xml.

إذا كنت تستخدم إضافات النية، ويكشف عن الاتصال PendingIntent.getActivity() مع العلم PendingIntent.FLAG_UPDATE_CURRENT, ، وإلا سيتم إعادة استخدام الإضافات نفسها لكل إشعار.

نصائح أخرى

كان لدي مشكلة مماثلة يعرض طلبي إعلامات الرسائل. عندما يكون هناك إشعارات متعددة والنقر فوق كل إشعار يعرض تفاصيل الإعلام في نشاط رسالة عرض. لقد حل مشكلة نفس المعلمات الإضافية التي يتم استلامها في نية الرسائل.

هنا هو الرمز الذي إصلاح هذا. رمز لإنشاء نية الإعلام.

 Intent notificationIntent = new Intent(getApplicationContext(), viewmessage.class);
    notificationIntent.putExtra("NotificationMessage", notificationMessage);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(getApplicationContext(),notificationIndex,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.setLatestEventInfo(getApplicationContext(), notificationTitle, notificationMessage, pendingNotificationIntent);

رمز عرض نشاط الرسالة.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    onNewIntent(getIntent());
}

@Override
public void onNewIntent(Intent intent){
    Bundle extras = intent.getExtras();
    if(extras != null){
        if(extras.containsKey("NotificationMessage"))
        {
            setContentView(R.layout.viewmain);
            // extract the extra-data in the Notification
            String msg = extras.getString("NotificationMessage");
            txtView = (TextView) findViewById(R.id.txtMessage);
            txtView.setText(msg);
        }
    }


}

ربما متأخرة بعض الشيء، ولكن: بدلا من هذا:

public void onNewIntent(Intent intent){
    Bundle extras = intent.getExtras();
    Log.i( "dbg","onNewIntent");

    if(extras != null){
        Log.i( "dbg", "Extra6 bool: "+ extras.containsKey("net.dbg.android.fjol"));
        Log.i( "dbg", "Extra6 val : "+ extras.getString("net.dbg.android.fjol"));

    }
    mTabsController.setActiveTab(TabsController.TAB_DOWNLOADS);
}

استخدم هذا:

Bundle extras = getIntent().getExtras();
if(extras !=null) {
    String value = extras.getString("keyName");
}

مواجهة نفس المشكلة هنا. أقوم بحلها باستخدام رمز الطلب المختلفة، استخدم معرف نفس الإشعار، أثناء إنشاء رينفاندي. ولكن لا يزال لا يعرف لماذا يجب أن يتم ذلك.

PendingIntent contentIntent = PendingIntent.getActivity(context, **id**, notificationIntent, 0);
notif.contentIntent = contentIntent;
nm.notify(**id**, notif);

بعد قراءة بعض قوائم البريد الإلكتروني وغيرها من المنتديات التي وجدتها أن الخدعة تبدو تضيف بيانات فريدة من نوعها إلى النية.

مثله:

   Intent notificationIntent = new Intent(Main.this, Main.class);
   notificationIntent.putExtra("sport_id", "sport"+id);
   notificationIntent.putExtra("game_url", "gameURL"+id);

   notificationIntent.setData((Uri.parse("foobar://"+SystemClock.elapsedRealtime()))); 

أنا لا أفهم لماذا يجب القيام بذلك، وحصلت على ما يجب تحديد شيء مع عدم القدرة على التعرف فقط بواسطة إضافاته ...

حاولت كل شيء ولكن لا شيء يعمل.

في نهاية المطاف جاء مع الحل التالي.

1- في بيان واضحة للنشاط أندرويد: launchmode = "singletop"

2- أثناء جعل النية المعلقة تفعل ما يلي، استخدم حزمة بدلا من استخدام Netent.putstring مباشرة () أو Intent.Putilt ()

                    Intent notificationIntent = new Intent(getApplicationContext(), CourseActivity.class);

                    Bundle bundle = new Bundle();
                    bundle.putString(Constants.EXAM_ID,String.valueOf(lectureDownloadStatus.getExamId()));
                    bundle.putInt(Constants.COURSE_ID,(int)lectureDownloadStatus.getCourseId());
                    bundle.putString(Constants.IMAGE_URL,lectureDownloadStatus.getImageUrl());

                    notificationIntent.putExtras(bundle);

                    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                            Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(),
                            new Random().nextInt(), notificationIntent,
                            PendingIntent.FLAG_UPDATE_CURRENT); 

androidmanifest.xml.

تشمل launchmode = "singletop"

<activity android:name=".MessagesDetailsActivity"
        android:launchMode="singleTop"
        android:excludeFromRecents="true"
        />

smsreceiver.java.

اضبط الأعلام على النوايا والانتقائية

Intent intent = new Intent(context, MessagesDetailsActivity.class);
    intent.putExtra("smsMsg", smsObject.getMsg());
    intent.putExtra("smsAddress", smsObject.getAddress());
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent contentIntent = PendingIntent.getActivity(context, notification_id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

messagedetailsactivity.java.

OnResume () - يسمى في كل مرة، تحميل الإضافات.

Intent intent = getIntent();
    String extraAddress = intent.getStringExtra("smsAddress");
    String extraBody = intent.getStringExtra("smsMsg");

آمل أن يساعد ذلك، استند إلى إجابات أخرى هنا على Stackoverflow، ولكن هذا هو الأكثر تحديثا يعمل بالنسبة لي.

إنه سهل، هذا هو الحل الخاص بي باستخدام الكائنات!

بلدي pojo.

public class Person implements Serializable{

    private String name;
    private int age;

    //get & set

}

طريقة الإعلام

  Person person = new Person();
  person.setName("david hackro");
  person.setAge(10);

    Intent notificationIntent = new Intent(this, Person.class);
    notificationIntent.putExtra("person",person);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.notification_icon)
                .setAutoCancel(true)
                .setColor(getResources().getColor(R.color.ColorTipografiaAdeudos))
                .setPriority(2)
                .setLargeIcon(bm)
                .setTicker(fotomulta.getTitle())
                .setContentText(fotomulta.getMessage())
                .setContentIntent(PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT))
                .setWhen(System.currentTimeMillis())
                .setContentTitle(fotomulta.getTicketText())
                .setDefaults(Notification.DEFAULT_ALL);

نشاط جديد

 private Person person;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notification_push);
    person = (Person) getIntent().getSerializableExtra("person");
}

حظا طيبا وفقك الله!!

بعد القيام ببعض البحث حصلت على حل من دليل مطور أندرويد

PendingIntent contentIntent ;
Intent intent = new Intent(this,TestActivity.class);
intent.putExtra("extra","Test");
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

stackBuilder.addParentStack(ArticleDetailedActivity.class);

contentIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

للحصول على قيمة إضافية نية في فئة نشاط الاختبار تحتاج إلى كتابة التعليمات البرمجية التالية:

 Intent intent = getIntent();
 String extra = intent.getStringExtra("extra") ;

إذا كنت تستخدم

android:taskAffinity="myApp.widget.notify.activity"
android:excludeFromRecents="true"

في ملف Androidmanifest.xml الخاص بك للنشاط للإطلاق، يجب عليك استخدام ما يلي في نيتكتك:

Intent notificationClick = new Intent(context, NotifyActivity.class);
    Bundle bdl = new Bundle();
    bdl.putSerializable(NotifyActivity.Bundle_myItem, myItem);
    notificationClick.putExtras(bdl);
    notificationClick.setData(Uri.parse(notificationClick.toUri(Intent.URI_INTENT_SCHEME) + myItem.getId()));
    notificationClick.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);  // schließt tasks der app und startet einen seperaten neuen

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(NotifyActivity.class);
    stackBuilder.addNextIntent(notificationClick);

    PendingIntent notificationPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(notificationPendingIntent);

المهم هو وضع بيانات فريدة على سبيل المثال باستخدام معرف فريد مثل:

notificationClick.setData(Uri.parse(notificationClick.toUri(Intent.URI_INTENT_SCHEME) + myItem.getId()));

للاستخدام الخدمات PendingIntent.FLAG_UPDATE_CURRENT

بالنسبة لي العمل.

يرجى استخدام الرصيف أثناء إظهار الإخطار مما سيتم حله.

نية رينفتينتس = رينف رينتيني. getactactivity (هذا، 0، checificittent، pendintent.flag_update_current)؛

أضف pendingintent.flag_update_current كحرر آخر.

في تنفيذ الإشعارات الخاص بك، استخدم رمز مثل هذا:

NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
...
Intent intent = new Intent(this, ExampleActivity.class);
intent.putExtra("EXTRA_KEY", "value");

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
nBuilder.setContentIntent(pendingIntent);
...

للحصول على قيم إضافية نية في الفحص البدائل، استخدم التعليمات البرمجية التالية:

...
Intent intent = getIntent();
if(intent!=null) {
    String extraKey = intent.getStringExtra("EXTRA_KEY");
}
...

ملاحظة مهمة جدا: ال نية :: putextra () الطريقة هي واحدة مثالية. للحصول على مفتاح إضافي، تحتاج إلى استخدام نية :: الحصول على [النوع] إضافي () طريقة.

ملحوظة: إخطار_id. و notification_channel_id. هي ثوابت أعلن في الفوضى

G'day، أنا أيضا أستطيع أن أقول أنني جربت كل شيء مذكور في هذه الوظائف وبضعة أماكن أخرى من مكان آخر. المشكلة رقم 1 بالنسبة لي هي أن النية الجديدة لديها دائما حزمة فارغة. كانت مشكلتي في التركيز كثيرا على تفاصيل "هل قمت بتضمينها. هذا أو. هذا". كان الحل الخاص بي في اتخاذ خطوة من التفاصيل والنظر في الهيكل العام للإخطار. عندما فعلت ذلك تمكنت من وضع الأجزاء الرئيسية من التعليمات البرمجية في التسلسل الصحيح. لذلك، إذا كنت تواجه مشكلات مماثلة تحقق من:

1. Intent notificationIntent = new Intent(MainActivity.this, NotificationActivity.class);

2a. Bundle bundle = new Bundle();

// أحب تحديد نوع البيانات أفضل بكثير. على سبيل المثال bundle.putint.

2b. notificationIntent.putExtras(bundle);
3. PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, WIZARD_NOTIFICATION_ID, notificationIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
4. NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
5.          NotificationCompat.Builder nBuilder =
                    new NotificationCompat.Builder(this)
                            .setSmallIcon(R.drawable.ic_notify)
                            .setContentTitle(title)
                            .setContentText(content)
                            .setContentIntent(contentIntent)
                            .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
                            .setAutoCancel(false)//false is standard. true == automatically removes the notification when the user taps it.
                            .setColor(getResources().getColor(R.color.colorPrimary))
                            .setCategory(Notification.CATEGORY_REMINDER)
                            .setPriority(Notification.PRIORITY_HIGH)
                            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
            notificationManager.notify(WIZARD_NOTIFICATION_ID, nBuilder.build());

مع هذا تسلسل أحصل على حزمة صالحة.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top